[ASan/Win] Add a comment about DCL-using-static vs threads
[blocksruntime.git] / test / tsan / static_init3.cc
blob94934b4a9f1a5dff2d922dc925c62b2c7c7cf73b
1 // RUN: %clangxx_tsan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <sched.h>
7 struct Cache {
8 int x;
9 };
11 Cache g_cache;
13 Cache *CreateCache() {
14 g_cache.x = rand();
15 return &g_cache;
18 _Atomic(Cache*) queue;
20 void *Thread1(void *x) {
21 static Cache *c = CreateCache();
22 __c11_atomic_store(&queue, c, 0);
23 return 0;
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();
34 if (c->x >= RAND_MAX)
35 exit(1);
36 return 0;
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);
47 // CHECK: WARNING: ThreadSanitizer: data race