source: CIVL/examples/pthread/llnl/bug6.c@ bb03188

main test-branch
Last change on this file since bb03188 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[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]20int *a, *b;
21long sum=0;
22
23void *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
48int main (int argc, char *argv[])
49{
50long i;
51void *status;
52pthread_t threads[NUMTHRDS];
53pthread_attr_t attr;
54
55/* Assign storage and initialize values */
56a = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
57b = (int*) malloc (NUMTHRDS*VECLEN*sizeof(int));
58
59for (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 */
66pthread_attr_init(&attr);
67pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
68for(i=0; i<NUMTHRDS; i++)
69 pthread_create(&threads[i], &attr, dotprod, (void *)i);
70
71pthread_attr_destroy(&attr);
72
73/* Wait on the threads for final result */
74for(i=0; i<NUMTHRDS; i++)
75 pthread_join(threads[i], &status);
76
77/* After joining, print out the results and cleanup */
78printf ("Final Global Sum=%li\n",sum);
79free (a);
80free (b);
81pthread_exit(NULL);
82}
83
84
Note: See TracBrowser for help on using the repository browser.