• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_tsan -O1 %s -o %t && not %t 2>&1 | 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 
Thread(void * x)12 void *Thread(void *x) {
13   read(fd, &buf, 1);
14   return NULL;
15 }
16 
main()17 int main() {
18   fd = open("/dev/random", O_RDONLY);
19   if (fd < 0) return 1;
20   pthread_t t[2];
21   pthread_create(&t[0], NULL, Thread, NULL);
22   pthread_create(&t[1], NULL, Thread, NULL);
23   pthread_join(t[0], NULL);
24   pthread_join(t[1], NULL);
25   close(fd);
26 }
27 
28 // CHECK: WARNING: ThreadSanitizer: data race
29 // CHECK:   Write of size 1
30 // CHECK:     #0 read
31 // CHECK:   Previous write of size 1
32 // CHECK:     #0 read
33