/* thread11m.c run 11 threads, 11 controlled parallel workers */ /* variable time in workers, waste */ /* threadID 0 for main, 1, 2, 3, ... 11 for workers */ /* using mutex.h and mutex.c */ #include "mutex.h" #include #include #include #include #define n_thread 11 #define waste 10000000 static int results[n_thread]; void * worker_code(void * threadID) { int i, j; double a = 2.0; i = (int)(long int)threadID; printf(" thread %d started, waiting \n", i); fflush(stdout); worker_init(i); // worker_barrier(i); printf(" thread %d computing \n", i); fflush(stdout); for(j=0; j<(i+1)*waste; j++) a = exp(1.000000001*log(a)); results[i] += 10*(i+1); printf(" thread %d finished computing, a=%g \n", i, a); fflush(stdout); worker_barrier(i); printf(" thread %d computing again \n", i); fflush(stdout); a = 2.0; for(j=0; j<(i+1)*waste; j++) a = exp(1.00000001*log(a)); results[i] += 100*(i+1); printf(" thread %d finished computing again, a=%g \n", i, a); fflush(stdout); worker_barrier(i); printf(" thread %d computing third \n", i); fflush(stdout); a = 2.0; for(j=0; j<(i+1)*waste; j++) a = exp(1.000000001*log(a)); results[i] += 1000*(i+1); printf(" thread %d finished computing third, a=%g \n", i, a); fflush(stdout); worker_barrier(i); printf(" thread %d computing fourth \n", i); fflush(stdout); a = 2.0; for(j=0; j<(i+1)*waste; j++) a = exp(1.000000001*log(a)); results[i] += 10000*(i+1); printf(" thread %d finished computing fourth, a=%g \n", i, a); fflush(stdout); worker_barrier(i); pthread_exit(NULL); /* done */ } /* end worker_code */ int main(int argc, char * argv[]) { int rc, t; double wallstart, wallnow; double cpustart, cpunow; printf("pthread11m.c running \n"); wallstart = (double)time(NULL); cpustart = (double)clock()/(double)CLOCKS_PER_SEC; mutex_init(n_thread); master_init(worker_code); /* n_thread threads */ fflush(stdout); printf("let all workers run, first time\n"); fflush(stdout); /* let threads run */ master_release(); printf("waiting for threads to be done computing first\n"); fflush(stdout); master_barrier(); printf("all threads finished first task \n"); fflush(stdout); for(t=0; t