55 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s2#include "test.h"3 4int fds[2];5 6void *ThreadCreatePipe(void *x) {7 pipe(fds);8 return NULL;9}10 11void *ThreadDummy(void *x) {12 return NULL;13}14 15void *ThreadWrite(void *x) {16 write(fds[1], "a", 1);17 barrier_wait(&barrier);18 return NULL;19}20 21void *ThreadClose(void *x) {22 barrier_wait(&barrier);23 close(fds[0]);24 close(fds[1]);25 return NULL;26}27 28int main() {29 barrier_init(&barrier, 2);30 pthread_t t_create;31 pthread_create(&t_create, NULL, ThreadCreatePipe, NULL);32 pthread_join(t_create, NULL);33 34 for (int i = 0; i < 100; i++) {35 pthread_t t_dummy;36 pthread_create(&t_dummy, NULL, ThreadDummy, NULL);37 pthread_join(t_dummy, NULL);38 }39 40 pthread_t t[2];41 pthread_create(&t[0], NULL, ThreadWrite, NULL);42 pthread_create(&t[1], NULL, ThreadClose, NULL);43 pthread_join(t[0], NULL);44 pthread_join(t[1], NULL);45}46 47// CHECK-NOT: CHECK failed48// CHECK: WARNING: ThreadSanitizer: data race49// CHECK: Write of size 850// CHECK: #0 close51// CHECK: #1 ThreadClose52// CHECK: Previous read of size 853// CHECK: #0 write54// CHECK: #1 ThreadWrite55