1 2 #include <pthread.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 /* Simple test program, has a race. Parent and child both modify y 7 with no locking. This is the program shown in Fig 2 of the 8 original Eraser paper by Savage et al. */ 9 10 int y = 0, v = 0; 11 pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; 12 13 void* child_fn ( void* arg ) 14 { 15 /* "Thread 2" in the paper */ 16 pthread_mutex_lock( &mu ); 17 v = v + 1; 18 pthread_mutex_unlock( &mu ); 19 y = y + 1; 20 return NULL; 21 } 22 23 int main ( void ) 24 { 25 pthread_t child; 26 27 if (pthread_create(&child, NULL, child_fn, NULL)) { 28 perror("pthread_create"); 29 exit(1); 30 } 31 32 /* "Thread 1" in the paper */ 33 y = y + 1; 34 pthread_mutex_lock( &mu ); 35 v = v + 1; 36 pthread_mutex_unlock( &mu ); 37 38 if (pthread_join(child, NULL)) { 39 perror("pthread join"); 40 exit(1); 41 } 42 43 return 0; 44 } 45