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#include <sched.h>
6
7struct Cache {
8 int x;
9 explicit Cache(int x)
10 : x(x) {
11 }
12};
13
14void *AsyncInit(void *p) {
15 return new Cache((int)(long)p);
16}
17
18Cache *CreateCache() {
19 pthread_t t;
20 pthread_create(newthread: &t, attr: 0, start_routine: AsyncInit, arg: (void*)(long)rand());
21 void *res;
22 pthread_join(th: t, thread_return: &res);
23 return (Cache*)res;
24}
25
26void *Thread1(void *x) {
27 static Cache *c = CreateCache();
28 if (c->x >= RAND_MAX)
29 exit(status: 1);
30 return 0;
31}
32
33int main() {
34 pthread_t t[2];
35 pthread_create(newthread: &t[0], attr: 0, start_routine: Thread1, arg: 0);
36 pthread_create(newthread: &t[1], attr: 0, start_routine: Thread1, arg: 0);
37 pthread_join(th: t[0], thread_return: 0);
38 pthread_join(th: t[1], thread_return: 0);
39 fprintf(stderr, format: "PASS\n");
40}
41
42// CHECK-NOT: WARNING: ThreadSanitizer: data race
43

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