Kostya Serebryany | ff15ef0 | 2012-05-10 14:18:22 +0000 | [diff] [blame^] | 1 | #include <pthread.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <sched.h> |
| 5 | |
| 6 | struct Cache { |
| 7 | int x; |
| 8 | Cache(int x) |
| 9 | : x(x) { |
| 10 | } |
| 11 | }; |
| 12 | |
| 13 | void *AsyncInit(void *p) { |
| 14 | return new Cache((int)(long)p); |
| 15 | } |
| 16 | |
| 17 | Cache *CreateCache() { |
| 18 | pthread_t t; |
| 19 | pthread_create(&t, 0, AsyncInit, (void*)rand()); |
| 20 | void *res; |
| 21 | pthread_join(t, &res); |
| 22 | return (Cache*)res; |
| 23 | } |
| 24 | |
| 25 | void *Thread1(void *x) { |
| 26 | static Cache *c = CreateCache(); |
| 27 | if (c->x >= RAND_MAX) |
| 28 | exit(1); |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | int main() { |
| 33 | pthread_t t[2]; |
| 34 | pthread_create(&t[0], 0, Thread1, 0); |
| 35 | pthread_create(&t[1], 0, Thread1, 0); |
| 36 | pthread_join(t[0], 0); |
| 37 | pthread_join(t[1], 0); |
| 38 | } |
| 39 | |
| 40 | // CHECK-NOT: WARNING: ThreadSanitizer: data race |