/* Try number 2 using pthreads * works, but terrible performance - lock contention * compile with: * * gcc count3s_parallel1.c -o count3s_parallel1 -pthread * * */ #include #include #include // changed by command line arg int num_threads = 1; // array to search - contains 9 threes int arr_length = 32; int arr[] = {2,3,0,2,3,3,1,0,0,1,3,2,2,3,1,0, 1,0,0,2,0,3,1,0,3,1,0,2,3,3,1,0}; // total of all threes found int count=0; pthread_mutex_t m; // mutex to lock count while in use // args - long id void count3s_thread(void *id) { int i; // compute the portion of the array that this thread should // work on int length_per_thread = arr_length / num_threads; int start = (long)id * length_per_thread; //printf("Start from thread %d: %d\n", (long)id, start); for(i=start;i