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 x 7 with no locking. */ 8 9 int x = 0; 10 11 void* child_fn ( void* arg ) 12 { 13 /* Unprotected relative to parent */ 14 x++; 15 return NULL; 16 } 17 18 int main ( void ) 19 { 20 const struct timespec delay = { 0, 100 * 1000 * 1000 }; 21 pthread_t child; 22 if (pthread_create(&child, NULL, child_fn, NULL)) { 23 perror("pthread_create"); 24 exit(1); 25 } 26 nanosleep(&delay, 0); 27 /* Unprotected relative to child */ 28 x++; 29 30 if (pthread_join(child, NULL)) { 31 perror("pthread join"); 32 exit(1); 33 } 34 35 return 0; 36 } 37