• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 
9 int fd;
10 char buf;
11 
Thread1(void * x)12 void *Thread1(void *x) {
13   buf = 1;
14   sleep(1);
15   return NULL;
16 }
17 
Thread2(void * x)18 void *Thread2(void *x) {
19   write(fd, &buf, 1);
20   return NULL;
21 }
22 
main()23 int main() {
24   fd = open("/dev/null", O_WRONLY);
25   if (fd < 0) return 1;
26   pthread_t t[2];
27   pthread_create(&t[0], NULL, Thread1, NULL);
28   sleep(1);
29   pthread_create(&t[1], NULL, Thread2, NULL);
30   pthread_join(t[0], NULL);
31   pthread_join(t[1], NULL);
32   close(fd);
33 }
34 
35 // CHECK: WARNING: ThreadSanitizer: data race
36 // CHECK:   Read of size 1
37 // CHECK:     #0 write
38 // CHECK:   Previous write of size 1
39 // CHECK:     #0 Thread1
40