• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
racer(void * p)10 static void *racer(void *p) {
11   *(int*)p = 42;
12   return 0;
13 }
14 
main()15 int main() {
16   switch (fork()) {
17   default:  // parent
18     while (wait(0) < 0) {}
19     break;
20   case 0:  // child
21     {
22       int x = 0;
23       pthread_t th1, th2;
24       pthread_create(&th1, 0, racer, &x);
25       pthread_create(&th2, 0, racer, &x);
26       pthread_join(th1, 0);
27       pthread_join(th2, 0);
28       exit(0);
29       break;
30     }
31   case -1:  // error
32     fprintf(stderr, "failed to fork (%d)\n", errno);
33     exit(1);
34   }
35   fprintf(stderr, "OK\n");
36 }
37 
38 // CHECK: ThreadSanitizer: data race
39 // CHECK: OK
40 
41