/***************************************************************************** * SOURCE: This is a translation of a Pthread program from the Lawrence Livermore * Computing Center POSIX Threads Programming Exercise at: * https://computing.llnl.gov/tutorials/pthreads/exercise.html * FILE: detached.cvl * DESCRIPTION: * This example demonstrates how to explicitly create a thread in a * detached state. This might be done to conserve some system resources * if the thread never needs to join later. Compare with the join.cvl program * where the threads are created joinable. * Command line execution: * civl verify -inputNUM_THREADS=4 detached.cvl ******************************************************************************/ #include "pthread.cvh" #include #include #include #include "math.cvh" $input int NUM_THREADS; void *BusyWork(void *t) { long i,tid; double result=0.0; tid = (long)*t; printf("Thread %d starting...\n",tid); for (i=0; i<1000000; i++) { result = result + sin(i) * tan(i); } printf("Thread %d done. Result = %e\n",tid, result); pthread_exit(NULL, false, NULL, 0); } int main(void) { pthread_t thread[NUM_THREADS]; pthread_attr_t attr; int rc; long t; long j[NUM_THREADS]; /* Initialize and set thread detached attribute */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); for(t=0;t