llvm-project
37 строк · 988.0 Байт
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4int Global;
5pthread_mutex_t mtx;
6
7void *Thread1(void *x) {
8pthread_mutex_lock(&mtx);
9Global++;
10pthread_mutex_unlock(&mtx);
11barrier_wait(&barrier);
12return NULL;
13}
14
15void *Thread2(void *x) {
16barrier_wait(&barrier);
17Global--;
18return NULL;
19}
20
21int main() {
22barrier_init(&barrier, 2);
23// CHECK: WARNING: ThreadSanitizer: data race
24// CHECK: Write of size 4 at {{.*}} by thread T2:
25// CHECK: Previous write of size 4 at {{.*}} by thread T1
26// CHECK: (mutexes: write [[M1:M[0-9]+]]):
27// CHECK: Mutex [[M1]] (0x{{.*}}) created at:
28// CHECK: #0 pthread_mutex_init
29// CHECK: #1 main {{.*}}mutexset2.cpp:[[@LINE+1]]
30pthread_mutex_init(&mtx, 0);
31pthread_t t[2];
32pthread_create(&t[0], NULL, Thread1, NULL);
33pthread_create(&t[1], NULL, Thread2, NULL);
34pthread_join(t[0], NULL);
35pthread_join(t[1], NULL);
36pthread_mutex_destroy(&mtx);
37}
38