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 */
13 pthread_t tid
[NUM_THREADS
]; /* array of thread IDs */
17 main( int argc
, char *argv
[] )
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
);
35 void * adder(void * parm
)
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!! */