llvm-project
35 строк · 864.0 Байт
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4void *Thread1(void *x) {
5int *p = (int*)x;
6p[0] = 1;
7barrier_wait(&barrier);
8return NULL;
9}
10
11void *Thread2(void *x) {
12barrier_wait(&barrier);
13char *p = (char*)x;
14p[2] = 1;
15return NULL;
16}
17
18int main() {
19barrier_init(&barrier, 2);
20int *data = new int(42);
21print_address("ptr1=", 1, data);
22print_address("ptr2=", 1, (char*)data + 2);
23pthread_t t[2];
24pthread_create(&t[0], NULL, Thread1, data);
25pthread_create(&t[1], NULL, Thread2, data);
26pthread_join(t[0], NULL);
27pthread_join(t[1], NULL);
28delete data;
29}
30
31// CHECK: ptr1=[[PTR1:0x[0-9,a-f]+]]
32// CHECK: ptr2=[[PTR2:0x[0-9,a-f]+]]
33// CHECK: WARNING: ThreadSanitizer: data race
34// CHECK: Write of size 1 at [[PTR2]] by thread T2:
35// CHECK: Previous write of size 4 at [[PTR1]] by thread T1:
36