// run a function in parallel N times, wait for the first one to finish // // run time is minimum of the run times for each function call // // main() uses an excessive amount of CPU time // #include #include // for sched_yield #include "common.c" // start the threads // pthread_t t[N]; for( int i = 0; i < N; ++i) if( pthread_create( &t[i], 0, f, &D[i].i ) ) { perror( "pthread_create"); return 1; } // wait until any one thread is finished // while(1) { int done = 0; for( int i = 0; i < N; ++i) if( D[i].size == 0) ++done; if( done > 0) break; // at least one thread is done sched_yield(); // wait a while } // stop all threads // for( int i = 0; i < N; ++i) pthread_cancel( t[i]); }