/* * print4.c -- forks off four threads that print their ids */ #include #include unsigned int Ego() { unsigned int return_val; return_val = (unsigned int)pthread_self(); return(return_val); } void *printme(void *arg) { printf("Hi. I'm thread %u\n", Ego()); return NULL; } main() { unsigned int i, vals[4]; pthread_t tids[4]; void *retval; for (i = 0; i < 4; i++) { if (pthread_create(tids+i, NULL, printme, NULL)!=0){ perror("on create"); exit(0); } } printme(NULL); /* main thread */ for (i = 0; i < 4; i++) { printf("I'm %u Trying to join with thread %u\n", Ego(),tids[i]); pthread_join(tids[i], &retval); printf("%u Joined with thread %u\n", Ego(),tids[i]); } }