37 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s2#include "test.h"3 4pthread_rwlock_t rwlock;5int GLOB;6 7void *Thread1(void *p) {8 (void)p;9 pthread_rwlock_rdlock(&rwlock);10 barrier_wait(&barrier);11 // Write under reader lock.12 GLOB++;13 pthread_rwlock_unlock(&rwlock);14 return 0;15}16 17int main(int argc, char *argv[]) {18 barrier_init(&barrier, 2);19 pthread_rwlock_init(&rwlock, NULL);20 pthread_rwlock_rdlock(&rwlock);21 pthread_t t;22 pthread_create(&t, 0, Thread1, 0);23 volatile int x = GLOB;24 (void)x;25 pthread_rwlock_unlock(&rwlock);26 barrier_wait(&barrier);27 pthread_join(t, 0);28 pthread_rwlock_destroy(&rwlock);29 return 0;30}31 32// CHECK: WARNING: ThreadSanitizer: data race33// CHECK: Write of size 4 at {{.*}} by thread T1{{.*}}:34// CHECK: #0 Thread1(void*) {{.*}}write_in_reader_lock.cpp:1235// CHECK: Previous read of size 4 at {{.*}} by main thread{{.*}}:36// CHECK: #0 main {{.*}}write_in_reader_lock.cpp:2337