| [506de9d] | 1 | /*****************************************************************************
|
|---|
| 2 | * FILE: bug6.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * This example demonstrates a race condition with a global variable that
|
|---|
| 5 | * gives obviously wrong results. Figure out how to fix the problem - or see
|
|---|
| 6 | * bug6fix.c for one solution. The dotprod_mutex.c example provides a much
|
|---|
| 7 | * more efficient way of solving this problem than bug6fix.c (FYI).
|
|---|
| 8 | * SOURCE: 07/06/05 Blaise Barney
|
|---|
| 9 | * LAST REVISED: 01/29/09 Blaise Barney
|
|---|
| 10 | ******************************************************************************/
|
|---|
| 11 | #include <pthread.h>
|
|---|
| 12 | #include <stdio.h>
|
|---|
| 13 | #include <stdlib.h>
|
|---|
| 14 |
|
|---|
| 15 | /* Define global data where everyone can see them */
|
|---|
| [e182ee04] | 16 |
|
|---|
| [aef8b11] | 17 | #define NUMTHRDS 3
|
|---|
| 18 | #define VECLEN 4
|
|---|
| [e182ee04] | 19 |
|
|---|
| [506de9d] | 20 | int *a, *b;
|
|---|
| 21 | long sum=0;
|
|---|
| 22 |
|
|---|
| 23 | void *dotprod(void *arg)
|
|---|
| 24 | {
|
|---|
| 25 | /* Each thread works on a different set of data.
|
|---|
| 26 | * The offset is specified by the arg parameter. The size of
|
|---|
| 27 | * the data for each thread is indicated by VECLEN.
|
|---|
| 28 | */
|
|---|
| 29 | int i, start, end, offset, len;
|
|---|
| 30 | long tid = (long)arg;
|
|---|
| 31 | offset = tid;
|
|---|
| 32 | len = VECLEN;
|
|---|
| 33 | start = offset*len;
|
|---|
| 34 | end = start + len;
|
|---|
| 35 |
|
|---|
| 36 | /* Perform my section of the dot product */
|
|---|
| 37 | printf("thread: %ld starting. start=%d end=%d\n",tid,start,end-1);
|
|---|
| [e182ee04] | 38 | for (i=start; i<end ; i++){
|
|---|
| 39 | int tmp = sum;
|
|---|
| 40 | tmp += (a[i] * b[i]);
|
|---|
| 41 | sum = tmp;
|
|---|
| 42 | }
|
|---|
| [506de9d] | 43 | printf("thread: %ld done. Global sum now is=%li\n",tid,sum);
|
|---|
| 44 |
|
|---|
| 45 | pthread_exit((void*) 0);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | int main (int argc, char *argv[])
|
|---|
| 49 | {
|
|---|
| 50 | long i;
|
|---|
| 51 | void *status;
|
|---|
| 52 | pthread_t threads[NUMTHRDS];
|
|---|
| 53 | pthread_attr_t attr;
|
|---|
| 54 |
|
|---|
| 55 | /* Assign storage and initialize values */
|
|---|
| 56 | a = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
|
|---|
| 57 | b = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
|
|---|
| 58 |
|
|---|
| 59 | for (i=0; i<VECLEN*NUMTHRDS; i++)
|
|---|
| 60 | a[i]= b[i]=1;
|
|---|
| 61 |
|
|---|
| 62 | /* Create threads as joinable, each of which will execute the dot product
|
|---|
| 63 | * routine. Their offset into the global vectors is specified by passing
|
|---|
| 64 | * the "i" argument in pthread_create().
|
|---|
| 65 | */
|
|---|
| 66 | pthread_attr_init(&attr);
|
|---|
| 67 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 68 | for(i=0; i<NUMTHRDS; i++)
|
|---|
| 69 | pthread_create(&threads[i], &attr, dotprod, (void *)i);
|
|---|
| 70 |
|
|---|
| 71 | pthread_attr_destroy(&attr);
|
|---|
| 72 |
|
|---|
| 73 | /* Wait on the threads for final result */
|
|---|
| 74 | for(i=0; i<NUMTHRDS; i++)
|
|---|
| 75 | pthread_join(threads[i], &status);
|
|---|
| 76 |
|
|---|
| 77 | /* After joining, print out the results and cleanup */
|
|---|
| 78 | printf ("Final Global Sum=%li\n",sum);
|
|---|
| 79 | free (a);
|
|---|
| 80 | free (b);
|
|---|
| 81 | pthread_exit(NULL);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|