/***************************************************************************** * FILE: join.c * DESCRIPTION: * This example demonstrates how to "wait" for thread completions by using * the Pthread join routine. Threads are explicitly created in a joinable * state for portability reasons. Use of the pthread_exit status argument is * also shown. Compare to detached.c * AUTHOR: 8/98 Blaise Barney * LAST REVISED: 01/30/09 ******************************************************************************/ #include #include #include #include #define NUM_THREADS 4 void *BusyWork(void *t) { int i; long tid; double result=0.0; tid = (long)t; printf("Thread %ld starting...\n",tid); for (i=0; i<1000000; i++) { result = result + sin(i) * tan(i); } printf("Thread %ld done. Result = %e\n",tid, result); pthread_exit((void*) t); } int main (int argc, char *argv[]) { pthread_t thread[NUM_THREADS]; pthread_attr_t attr; int rc; long t; void *status; /* Initialize and set thread detached attribute */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); for(t=0; t