/***************************************************************************** * FILE: bug6fix.c * DESCRIPTION: * This solution uses a mutex variable to protect the global sum while each * thread updates it. A much more efficient method would be that used in the * dotprod_mutex.c example. * SOURCE: 07/06/05 Blaise Barney * LAST REVISED: 01/29/09 Blaise Barney ******************************************************************************/ #include #include #include /* Define global data where everyone can see them */ #define NUMTHRDS 3 #define VECLEN 4 pthread_mutex_t mutexsum; int *a, *b; long sum=0.0; void *dotprod(void *arg) { /* Each thread works on a different set of data. * The offset is specified by the arg parameter. The size of * the data for each thread is indicated by VECLEN. */ int i, start, end, offset, len; long tid; tid = (long)arg; offset = tid; len = VECLEN; start = offset*len; end = start + len; /* Perform my section of the dot product */ printf("thread: %ld starting. start=%d end=%d\n",tid,start,end-1); for (i=start; i