/***************************************************************************** * FILE: bug6.c * DESCRIPTION: * This example demonstrates a race condition with a global variable that * gives obviously wrong results. Figure out how to fix the problem - or see * bug6fix.c for one solution. The dotprod_mutex.c example provides a much * more efficient way of solving this problem than bug6fix.c (FYI). * 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 int *a, *b; long sum=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 = (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