// A well behaved version of "Hello World". // The random sleep() just causes different // interleavings, but each thread always gets // a distinct value of i. // // Compile: gcc hello.c -o hello -lpthread // Run: ./hello #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; pthread_t thread[P]; time_t t; t = time(NULL); // seed the random number srand((int) t); // generator from outside printf("Hello from the main thread\n"); for (i=0; i