39 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s2#include "test.h"3#include <sys/types.h>4#include <sys/stat.h>5#include <fcntl.h>6 7int fd;8char buf;9 10void *Thread1(void *x) {11 buf = 1;12 barrier_wait(&barrier);13 return NULL;14}15 16void *Thread2(void *x) {17 write(fd, &buf, 1);18 return NULL;19}20 21int main() {22 barrier_init(&barrier, 2);23 fd = open("/dev/null", O_WRONLY);24 if (fd < 0) return 1;25 pthread_t t[2];26 pthread_create(&t[0], NULL, Thread1, NULL);27 barrier_wait(&barrier);28 pthread_create(&t[1], NULL, Thread2, NULL);29 pthread_join(t[0], NULL);30 pthread_join(t[1], NULL);31 close(fd);32}33 34// CHECK: WARNING: ThreadSanitizer: data race35// CHECK: Read of size 136// CHECK: #0 write37// CHECK: Previous write of size 138// CHECK: #0 Thread139