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
6struct Cache {
7 int x;
8 explicit Cache(int x)
9 : x(x) {
10 }
11};
12
13void foo(Cache *my) {
14 static Cache *c = my ? my : new Cache(rand());
15 if (c->x >= RAND_MAX)
16 exit(status: 1);
17}
18
19void *Thread(void *x) {
20 foo(my: new Cache(rand()));
21 return 0;
22}
23
24int main() {
25 pthread_t t[2];
26 pthread_create(newthread: &t[0], attr: 0, start_routine: Thread, arg: 0);
27 pthread_create(newthread: &t[1], attr: 0, start_routine: Thread, arg: 0);
28 pthread_join(th: t[0], thread_return: 0);
29 pthread_join(th: t[1], thread_return: 0);
30 fprintf(stderr, format: "PASS\n");
31}
32
33// CHECK-NOT: WARNING: ThreadSanitizer: data race
34

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