// This version of "Hello World" doesn't behave! The problem // is that the "id" parameter is passed to the thread by // reference (ie as a pointer to its true location. But // that location is shared, and is being updated all the // time by the loops in the main function. Thus // the value actually picked up by sayhello depends upon // the interleaving of these events. The sleep() just // encourages the effect. // // Compile: gcc nasty.c -o nasty -lpthread // Run: ./nasty #include #include #include #include #define P 10 void *sayhello (void *id) { sleep(rand()%5); printf("Hello from thread %ld\n", *(long *)id); } int main (int argc, char *argv[]) { long i; time_t t; pthread_t thread[P]; t = time(NULL); // seed the random number srand((int) t); // geberator from outside printf("Hello from the main thread\n"); for (i=0; i