fd_pipe_race.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1// RUN: %clangxx_tsan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
2#include <pthread.h>
3#include <stdio.h>
4#include <unistd.h>
5
6int fds[2];
7
8void *Thread1(void *x) {
9  write(fds[1], "a", 1);
10  return NULL;
11}
12
13void *Thread2(void *x) {
14  sleep(1);
15  close(fds[0]);
16  close(fds[1]);
17  return NULL;
18}
19
20int main() {
21  pipe(fds);
22  pthread_t t[2];
23  pthread_create(&t[0], NULL, Thread1, NULL);
24  pthread_create(&t[1], NULL, Thread2, NULL);
25  pthread_join(t[0], NULL);
26  pthread_join(t[1], NULL);
27}
28
29// CHECK: WARNING: ThreadSanitizer: data race
30// CHECK:   Write of size 8
31// CHECK:     #0 close
32// CHECK:     #1 Thread2
33// CHECK:   Previous read of size 8
34// CHECK:     #0 write
35// CHECK:     #1 Thread1
36
37
38