| 1 | // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s |
|---|---|
| 2 | #include <pthread.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdio.h> |
| 5 | #include <sched.h> |
| 6 | |
| 7 | struct Cache { |
| 8 | int x; |
| 9 | explicit Cache(int x) |
| 10 | : x(x) { |
| 11 | } |
| 12 | }; |
| 13 | |
| 14 | int g_other; |
| 15 | |
| 16 | Cache *CreateCache() { |
| 17 | g_other = rand(); |
| 18 | return new Cache(rand()); |
| 19 | } |
| 20 | |
| 21 | void *Thread1(void *x) { |
| 22 | static Cache *c = CreateCache(); |
| 23 | if (c->x == g_other) |
| 24 | exit(status: 1); |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | int main() { |
| 29 | pthread_t t[2]; |
| 30 | pthread_create(newthread: &t[0], attr: 0, start_routine: Thread1, arg: 0); |
| 31 | pthread_create(newthread: &t[1], attr: 0, start_routine: Thread1, arg: 0); |
| 32 | pthread_join(th: t[0], thread_return: 0); |
| 33 | pthread_join(th: t[1], thread_return: 0); |
| 34 | fprintf(stderr, format: "PASS\n"); |
| 35 | } |
| 36 | |
| 37 | // CHECK-NOT: WARNING: ThreadSanitizer: data race |
| 38 |
