modified: tasks/fgi.wdl
[GalaxyCodeBases.git] / c_cpp / pthreadtest / 01raceexample.c
blobf1acb02cfeba9a5750c1d809eb5d0e3442be5561
1 /* raceexample.c */
2 /* Simple pthread race example */
3 /* THIS CODE HAS A BUG IN IT! */
4 /* To compile me for Linux, type: gcc -o filename filename.c -lpthread */
5 /* To execute, type: filename */
7 #include <pthread.h>
8 #include <stdio.h>
10 void * adder(void *);
12 #define NUM_THREADS 2
13 pthread_t tid[NUM_THREADS]; /* array of thread IDs */
15 int bignum = 0;
17 main( int argc, char *argv[] )
19 int i, ret;
21 //thr_setconcurrency(3);
22 for (i=0; i<NUM_THREADS; i++) {
23 pthread_create(&tid[i], NULL, adder, NULL);
25 for ( i = 0; i < NUM_THREADS; i++)
26 pthread_join(tid[i], NULL);
28 printf("main() reporting that all %d threads have terminated\n", i);
29 printf("I am main! bignum=%d\n", bignum);
31 } /* main */
35 void * adder(void * parm)
37 int i;
38 printf("I am a new thread!\n");
39 for(i=0;i<100000;i++) {
40 bignum++; /* BUG HERE: THIS IS NOT IN A MUTEX AND IS INCORRECT!! */
42 pthread_exit(0);