1 #include <pthread.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 /* Simple demonstration of lockset tracking at byte granularity. */ 6 7 char bytes[10]; 8 9 void* child_fn ( void* arg ) 10 { 11 int i; 12 for (i = 0; i < 5; i++) 13 bytes[2*i + 0] ++; /* child accesses: 0 2 4 6 8 */ 14 return NULL; 15 } 16 17 int main ( void ) 18 { 19 const struct timespec delay = { 0, 100 * 1000 * 1000 }; 20 int i; 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, but harmless, since different 28 bytes accessed */ 29 for (i = 0; i < 5; i++) 30 bytes[2*i + 1] ++; /* accesses: 1 3 5 7 9 */ 31 32 /* Unprotected relative to child, but harmful; same bytes */ 33 for (i = 0; i < 3; i++) 34 bytes[3*i + 1] ++; /* accesses: 1 4(race!) 7 */ 35 36 if (pthread_join(child, NULL)) { 37 perror("pthread join"); 38 exit(1); 39 } 40 41 return 0; 42 } 43