Add 469782 to NEWS
[valgrind.git] / helgrind / tests / tc16_byterace.c
blobf090b96a66bee1e3080bca6ed2904c840f169727
1 #include <pthread.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 /* Simple demonstration of lockset tracking at byte granularity. */
7 char bytes[10];
9 void* child_fn ( void* arg )
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;
17 int main ( void )
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);
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 */
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 */
36 if (pthread_join(child, NULL)) {
37 perror("pthread join");
38 exit(1);
41 return 0;