Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7033281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:02:46+00:00 2026-05-28T01:02:46+00:00

#include <pthread.h> #define NUM_THREADS 4 void *work(void *i){ printf(Hello, world from %i\n, pthread_self()); pthread_exit(NULL);

  • 0
#include <pthread.h>
#define NUM_THREADS 4

void *work(void *i){
    printf("Hello, world from %i\n", pthread_self());
    pthread_exit(NULL); 
}

int main(int argc, char **argv){
    int i;
    pthread_t id[NUM_THREADS];
    for(i = 0; i < NUM_THREADS; ++i){
       if(pthread_create(&id[i], NULL, work, NULL)){
          printf("Error creating the thread\n"); exit(19);
       } 
    }
    printf("After creating the thread. My id is: %i\n",
    pthread_self());
    return 0;
}

i know the output is :

Hello, world from 2
Hello, world from 3
After creating the thread. My id is: 1
Hello, world from …

First of all this is not a homework.
POSIX is not my field so I just want an explanation of the output (no need to explain functions used since I know what they do) some quick answers of:

  1. ARE the ids for pthreads (2 , 3 , 1) specified by the system??
  2. the ++i used … did it affect the output somehow?
  3. why is it that there are only 4 threads in the end (3 + main) why not 5?!
  4. why is it that after main printed After creating …. another thread executed??!! how come??!!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:02:47+00:00Added an answer on May 28, 2026 at 1:02 am
    1. I’m not sure whether it’s specified, but I believe (nearly?) all thread ids are started at 1 (the main) and incremented from there.
    2. No, ++i has nothing to do with the output.
    3. Since you didn’t pthread_join the threads, the main function (and therefore the program) exited before the last thread executed.
    4. I’m not sure precisely what you’re trying to ask here, but the answer is concurrency. There’s no guarantee of when or in what order the threads execute.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100
Consider the following program: #define _POSIX_C_SOURCE 200809L #include <time.h> #include <pthread.h> #include <signal.h> void
see i have code like #include<stdio.h> #include<pthread.h> #include<string.h> void* thread_function(void) { printf (This is
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits
#include <iostream> using namespace std; int main() { double u = 0; double w
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include<iostream> class name { public: int a; name():a(0){}; }; void add(name * pname) {
When compiling the following code: #include <iostream> #include <thread> using namespace std; void hello()
#include <stdio.h> #include <errno.h> #include <pthread.h> pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; void *func(void *arg) {
#include <vector> std::vector<long int> as; long int a(size_t n){ if(n==1) return 1; if(n==2) return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.