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 X; 10 11 void *Thread1(void *x) { 12 sleep(1); 13 int f = open("/dev/random", O_RDONLY); 14 char buf; 15 read(f, &buf, 1); 16 close(f); 17 X = 42; 18 return NULL; 19 } 20 21 void *Thread2(void *x) { 22 X = 43; 23 write(STDOUT_FILENO, "a", 1); 24 return NULL; 25 } 26 27 int main() { 28 pthread_t t[2]; 29 pthread_create(&t[0], NULL, Thread1, NULL); 30 pthread_create(&t[1], NULL, Thread2, NULL); 31 pthread_join(t[0], NULL); 32 pthread_join(t[1], NULL); 33 } 34 35 // CHECK: WARNING: ThreadSanitizer: data race 36 // CHECK: Write of size 4 37 // CHECK: #0 Thread1 38 // CHECK: Previous write of size 4 39 // CHECK: #0 Thread2 40 41 42