1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6
7int X;
8
9void *Thread1(void *x) {
10 barrier_wait(barrier: &barrier);
11 int f = open(file: "/dev/random", O_RDONLY);
12 char buf;
13 read(fd: f, buf: &buf, nbytes: 1);
14 close(fd: f);
15 X = 42;
16 return NULL;
17}
18
19void *Thread2(void *x) {
20 X = 43;
21 write(STDOUT_FILENO, buf: "a", n: 1);
22 barrier_wait(barrier: &barrier);
23 return NULL;
24}
25
26int main() {
27 barrier_init(barrier: &barrier, count: 2);
28 pthread_t t[2];
29 pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL);
30 pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL);
31 pthread_join(th: t[0], NULL);
32 pthread_join(th: 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

source code of compiler-rt/test/tsan/fd_stdout_race.cpp