llvm-project
42 строки · 1000.0 Байт
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"3
4// Ensure that we can restore a stack of a finished thread.
5
6int g_data;7
8void __attribute__((noinline)) foobar(int *p) {9*p = 42;10}
11
12void *Thread1(void *x) {13foobar(&g_data);14barrier_wait(&barrier);15return NULL;16}
17
18void *Thread2(void *x) {19barrier_wait(&barrier);20sleep(1); // let the thread finish and exit21g_data = 43;22return NULL;23}
24
25int main() {26barrier_init(&barrier, 2);27pthread_t t[2];28pthread_create(&t[0], NULL, Thread1, NULL);29pthread_create(&t[1], NULL, Thread2, NULL);30pthread_join(t[0], NULL);31pthread_join(t[1], NULL);32return 0;33}
34
35// CHECK: WARNING: ThreadSanitizer: data race
36// CHECK: Write of size 4 at {{.*}} by thread T2:
37// CHECK: Previous write of size 4 at {{.*}} by thread T1:
38// CHECK: #0 foobar
39// CHECK: #1 Thread1
40// CHECK: Thread T1 (tid={{.*}}, finished) created by main thread at:
41// CHECK: #0 pthread_create
42// CHECK: #1 main
43