30 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s2#include "test.h"3 4#include <fcntl.h>5 6void *Thread(void *x) {7 int fd = (long)x;8 char buf;9 read(fd, &buf, 1);10 barrier_wait(&barrier);11 close(fd);12 return NULL;13}14 15int main() {16 barrier_init(&barrier, 2);17 int fd = open("/dev/random", O_RDONLY);18 pthread_t t[2];19 pthread_create(&t[0], NULL, Thread, (void *)(long)fd);20 pthread_create(&t[1], NULL, Thread, (void *)(long)fd);21 22 pthread_join(t[0], NULL);23 pthread_join(t[1], NULL);24}25 26// CHECK: WARNING: ThreadSanitizer: data race27// CHECK: Location is file descriptor {{[0-9]+}} {{(destroyed by thread|created by main)}} 28// CHECK: #0 {{close|open}}29// CHECK: #1 {{Thread|main}}30