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(status: 1);
36 return 0;
37}
38
39int main() {
40 pthread_t t[2];
41 pthread_create(newthread: &t[0], attr: 0, start_routine: Thread1, arg: 0);
42 pthread_create(newthread: &t[1], attr: 0, start_routine: Thread2, arg: 0);
43 pthread_join(th: t[0], thread_return: 0);
44 pthread_join(th: t[1], thread_return: 0);
45}
46
47// CHECK: WARNING: ThreadSanitizer: data race
48

source code of compiler-rt/test/tsan/static_init3.cpp