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
7struct Cache {
8  int x;
9};
10
11Cache g_cache;
12
13Cache *CreateCache() {
14  g_cache.x = rand();
15  return &g_cache;
16}
17
18_Atomic(Cache*) queue;
19
20void *Thread1(void *x) {
21  static Cache *c = CreateCache();
22  __c11_atomic_store(&queue, c, 0);
23  return 0;
24}
25
26void *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
39int 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