1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | 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 };
10 
11 Cache g_cache;
12 
CreateCache()13 Cache *CreateCache() {
14   g_cache.x = rand();
15   return &g_cache;
16 }
17 
18 _Atomic(Cache*) queue;
19 
Thread1(void * x)20 void *Thread1(void *x) {
21   static Cache *c = CreateCache();
22   __c11_atomic_store(&queue, c, 0);
23   return 0;
24 }
25 
Thread2(void * x)26 void *Thread2(void *x) {
27   Cache *c = 0;
28   for (;;) {
29     c = __c11_atomic_load(&queue, 0);
30     if (c)
31       break;
32     sched_yield();
33   }
34   if (c->x >= RAND_MAX)
35     exit(1);
36   return 0;
37 }
38 
main()39 int main() {
40   pthread_t t[2];
41   pthread_create(&t[0], 0, Thread1, 0);
42   pthread_create(&t[1], 0, Thread2, 0);
43   pthread_join(t[0], 0);
44   pthread_join(t[1], 0);
45 }
46 
47 // CHECK: WARNING: ThreadSanitizer: data race
48