#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:
- ARE the ids for pthreads (2 , 3 , 1) specified by the system??
- the
++iused … did it affect the output somehow? - why is it that there are only 4 threads in the end (3 + main) why not 5?!
- why is it that after main printed After creating …. another thread executed??!! how come??!!
++ihas nothing to do with the output.pthread_jointhe threads, themainfunction (and therefore the program) exited before the last thread executed.