/* Compile Using: gcc -lpthread -Werror -Wall thread-create_return.c -o thread-create_return */ #include #include #include /* srand, rand */ #include long int sharedNumber=0; void* childThread (void* threadID) { int i; sleep((long)threadID);// REMOVE SLEEP() TO SEE THE EFFECT OF RACE CONDITION for(i=0;i<1000000;i++)sharedNumber++; printf ("Thread: Child Thread %ld has computed RANDOM NUMBER: %ld\n", (long)threadID,sharedNumber); pthread_exit((void*)sharedNumber); // Return a Value to Main: Parent } /* The main program. */ int main () { pthread_t thread_id[10]; /* Store the threadIDs in an array */ long threadCount; void *status; /* Create a new thread. The new thread will run the print_xs function. */ for (threadCount = 1; threadCount< 10; threadCount++){ pthread_create (&thread_id[threadCount], NULL, &childThread, (void *)threadCount); printf ("Parent has created thread %ld with thread_id %u \n", threadCount, (unsigned int)thread_id[threadCount]); //pthread_join(thread_id[threadCount], NULL); /* Join immediately after creation of the thread */ } printf ("Main: Parent has CREATED ALL thread.\n"); for (threadCount = 1; threadCount< 10; threadCount++){ /* Join after all thread creation */ pthread_join(thread_id[threadCount], &status); printf("Main: completed join with thread %ld having a status of %ld\n",threadCount,(long)status); } printf ("Parent has WAITED FOR ALL thread to finish.\n"); //sleep(1); return 0; }