4. Parallel Implementation - wait for all
pthread.c:// run a function in parallel N times, wait using pthread_join() // // run time is maximum of the run times for each function call // #include <pthread.h> #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 all threads are finished // for( int i = 0; i < N; ++i) pthread_join( t[i], 0); } --- $ gcc -std=c11 -pedantic -Wall -g -O -o pthread pthread.c -pthread $ time ./pthread f 0 start = 0, size = 6 f 3 start = 30, size = 3 f 1 start = 10, size = 5 f 2 start = 20, size = 4 f 3 done f 2 done f 1 done f 0 done real 0m6.004s user 0m0.000s sys 0m0.004s