1 2 #include <pthread.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 /* Simple test program, no race: parent only modified x after child 7 has modified it and then joined with the parent. Tests simple 8 thread lifetime segment handling. */ 9 10 int x = 0; 11 child_fn(void * arg)12void* child_fn ( void* arg ) 13 { 14 /* Unprotected relative to parent, but in child's segment only */ 15 x++; 16 return NULL; 17 } 18 main(void)19int main ( void ) 20 { 21 pthread_t child; 22 23 x++; /* happens in parent's segment */ 24 25 if (pthread_create(&child, NULL, child_fn, NULL)) { 26 perror("pthread_create"); 27 exit(1); 28 } 29 30 if (pthread_join(child, NULL)) { 31 perror("pthread join"); 32 exit(1); 33 } 34 35 /* Now back in parent's segment */ 36 x++; 37 38 return 0; 39 } 40