29 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -o %t && %env_tsan_opts=flush_memory_ms=1 %run %t 2>&1 | FileCheck %s2#include "test.h"3#include <fcntl.h>4#include <sys/stat.h>5#include <sys/types.h>6#include <unistd.h>7 8void *Thread(void *stop) {9 while (!__atomic_load_n((int *)stop, __ATOMIC_RELAXED))10 close(open("/dev/null", O_RDONLY));11 return 0;12}13 14int main() {15 int stop = 0;16 const int kThreads = 10;17 pthread_t th[kThreads];18 for (int i = 0; i < kThreads; i++)19 pthread_create(&th[i], 0, Thread, &stop);20 sleep(5);21 __atomic_store_n(&stop, 1, __ATOMIC_RELAXED);22 for (int i = 0; i < kThreads; i++)23 pthread_join(th[i], 0);24 fprintf(stderr, "DONE\n");25}26 27// CHECK-NOT: WARNING: ThreadSanitizer: data race28// CHECK: DONE29