1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include <stdlib.h>
3#include <stdio.h>
4#include <errno.h>
5#include <pthread.h>
6#include <unistd.h>
7#include <sys/types.h>
8#include <sys/wait.h>
9
10static void *racer(void *p) {
11 *(int*)p = 42;
12 return 0;
13}
14
15int main() {
16 switch (fork()) {
17 default: // parent
18 while (wait(stat_loc: 0) < 0) {}
19 break;
20 case 0: // child
21 {
22 int x = 0;
23 pthread_t th1, th2;
24 pthread_create(newthread: &th1, attr: 0, start_routine: racer, arg: &x);
25 pthread_create(newthread: &th2, attr: 0, start_routine: racer, arg: &x);
26 pthread_join(th: th1, thread_return: 0);
27 pthread_join(th: th2, thread_return: 0);
28 exit(status: 0);
29 break;
30 }
31 case -1: // error
32 fprintf(stderr, format: "failed to fork (%d)\n", errno);
33 exit(status: 1);
34 }
35 fprintf(stderr, format: "OK\n");
36}
37
38// CHECK: ThreadSanitizer: data race
39// CHECK: OK
40
41

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