jsgf | 12475f6 | 2003-10-16 06:09:41 +0000 | [diff] [blame] | 1 | /* test child thread inheriting data */ |
| 2 | |
nethercote | 766eb76 | 2004-04-16 07:22:42 +0000 | [diff] [blame] | 3 | // *** |
| 4 | // |
| 5 | // Helgrind should detect an error on line 48 for this test, but it doesn't! |
| 6 | // |
| 7 | // *** |
| 8 | |
jsgf | 12475f6 | 2003-10-16 06:09:41 +0000 | [diff] [blame] | 9 | #include <pthread.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | static volatile int shared[2]; |
| 13 | |
| 14 | static void *t1(void *v) |
| 15 | { |
| 16 | volatile int *ip = (int *)v; |
jsgf | 12475f6 | 2003-10-16 06:09:41 +0000 | [diff] [blame] | 17 | *ip += 44; |
| 18 | *ip *= 2; |
jsgf | 12475f6 | 2003-10-16 06:09:41 +0000 | [diff] [blame] | 19 | sleep(1); |
jsgf | 12475f6 | 2003-10-16 06:09:41 +0000 | [diff] [blame] | 20 | return 0; |
| 21 | } |
| 22 | |
| 23 | static void *t2(void *v) |
| 24 | { |
| 25 | volatile int *ip = (int *)v; |
jsgf | 12475f6 | 2003-10-16 06:09:41 +0000 | [diff] [blame] | 26 | *ip += 88; |
| 27 | *ip *= 3; |
jsgf | 12475f6 | 2003-10-16 06:09:41 +0000 | [diff] [blame] | 28 | sleep(2); |
jsgf | 12475f6 | 2003-10-16 06:09:41 +0000 | [diff] [blame] | 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | int main() |
| 33 | { |
| 34 | pthread_t a, b; |
| 35 | volatile int ret = 0; |
| 36 | |
| 37 | sleep(0); |
| 38 | |
| 39 | shared[0] = 22; |
| 40 | shared[1] = 77; |
| 41 | |
| 42 | pthread_create(&a, NULL, t1, (void *)&shared[0]); |
| 43 | pthread_create(&b, NULL, t2, (void *)&shared[1]); |
| 44 | |
| 45 | pthread_join(a, NULL); |
| 46 | |
| 47 | ret += shared[0]; /* no error - a is finished */ |
| 48 | ret += shared[1]; /* expect error - b has not finished, |
| 49 | so we can't touch shared[1] yet */ |
| 50 | |
| 51 | pthread_join(b, NULL); |
| 52 | |
| 53 | |
| 54 | return ret; |
| 55 | } |