/***************************************************************************** * SOURCE: This is a translation of a Pthread program from the Lawrence Livermore * Computing Center POSIX Threads Programming Exercise at: * https://computing.llnl.gov/tutorials/pthreads/exercise.html * FILE: bug6.cvl * DESCRIPTION: * This example demonstrates a race condition with a global variable that * gives obviously wrong results. 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). * Command line execution: * civl verify -inputNUMTHRDS=5 -inputVECLEN=10 bug6.cvl ******************************************************************************/ #include "pthread.cvh" #include #include #include /* Define global data where everyone can see them */ $input int NUMTHRDS; $input int VECLEN; 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; // Dereference rather than direct conv offset = tid; len = VECLEN; start = offset*len; end = start + len; /* Perform my section of the dot product */ printf("thread: %d starting. start=%d end=%d\n",tid,start,end-1); // Removed l from %ld, unsupported for (i=start; i