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 #include <unistd.h> 9 #include <errno.h> 10 11 int fd; 12 char buf; 13 14 void *Thread(void *x) { 15 sleep(1); 16 read(fd, &buf, 1); 17 return NULL; 18 } 19 20 int main() { 21 fd = open("/dev/random", O_RDONLY); 22 if (fd < 0) { 23 fprintf(stderr, "failed to open /dev/random (%d)\n", errno); 24 return 1; 25 } 26 pthread_t t[2]; 27 pthread_create(&t[0], NULL, Thread, NULL); 28 pthread_create(&t[1], NULL, Thread, NULL); 29 pthread_join(t[0], NULL); 30 pthread_join(t[1], NULL); 31 close(fd); 32 fprintf(stderr, "DONE\n"); 33 } 34 35 // CHECK: WARNING: ThreadSanitizer: data race 36 // CHECK: Write of size 1 37 // CHECK: #0 read 38 // CHECK: Previous write of size 1 39 // CHECK: #0 read 40 // CHECK: DONE 41 42