1 2 #include <pthread.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 /* Simple test program, has two races. A happens-before detector can only 7 ever detect one of them, though. XXX: apparently not so; Drd and H 3.4 detect both. */ 8 9 int unprot1 = 0, unprot2 = 0, prot = 0; 10 pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; 11 12 void* child_fn ( void* arg ) 13 { 14 unprot1 ++; 15 pthread_mutex_lock( &mu ); 16 prot ++; 17 pthread_mutex_unlock( &mu ); 18 unprot2 ++; 19 return NULL; 20 } 21 22 int main ( void ) 23 { 24 pthread_t child; 25 26 if (pthread_create(&child, NULL, child_fn, NULL)) { 27 perror("pthread_create"); 28 exit(1); 29 } 30 31 unprot1 ++; 32 pthread_mutex_lock( &mu ); 33 prot ++; 34 pthread_mutex_unlock( &mu ); 35 unprot2 ++; 36 37 if (pthread_join(child, NULL)) { 38 perror("pthread join"); 39 exit(1); 40 } 41 42 return 0; 43 } 44