// thread_loop.c run 4 threads n_thread, // each runs twice nloop, // each has synchronized start B1 // and three computations B2,B3,B4 for data transfer to main // np is number of cores needed, n_thread+1 #include #include #include #include #define n_thread 4 #define np 5 #define nloop 2 static int nb = 0; static int results[n_thread]; static pthread_cond_t go; static pthread_mutex_t bar; void barrier(void) { printf("in barrier \n"); fflush(stdout); pthread_mutex_lock(&bar); nb++; printf("nb=%d of np=%d \n", nb, np); fflush(stdout); if(nb == np) { nb = 0; printf("about to broadcast \n"); fflush(stdout); pthread_cond_broadcast(&go); } else { printf("about to cond wait \n"); fflush(stdout); pthread_cond_wait(&go, &bar); } pthread_mutex_unlock(&bar); } // end barrier void * thread_compute(void * threadID) { int i, iloop=0; // local variables in each thread i = (int)threadID; printf(" thread %d at first barrier, iloop %d \n", i, iloop); fflush(stdout); barrier(); // B1 for(iloop=0; iloop