Debian Codegraph @

gcc/testsuite/g++.dg/tsan/vptr_benign_race.C

raw
1 #include <pthread.h>
2 #include <semaphore.h>
3 #include <stdio.h>
4
5 struct A {
6 A() {
7 sem_init(&sem_, 0, 0);
8 }
9 virtual void F() {
10 }
11 void Done() {
12 sem_post(&sem_);
13 }
14 virtual ~A() {
15 }
16 sem_t sem_;
17 };
18
19 struct B : A {
20 virtual void F() {
21 }
22 virtual ~B() {
23 sem_wait(&sem_);
24 sem_destroy(&sem_);
25 }
26 };
27
28 static A *obj = new B;
29
30 void *Thread1(void *x) {
31 obj->F();
32 obj->Done();
33 return NULL;
34 }
35
36 void *Thread2(void *x) {
37 delete obj;
38 return NULL;
39 }
40
41 int main() {
42 pthread_t t[2];
43 pthread_create(&t[0], NULL, Thread1, NULL);
44 pthread_create(&t[1], NULL, Thread2, NULL);
45 pthread_join(t[0], NULL);
46 pthread_join(t[1], NULL);
47 fprintf(stderr, "PASS\n");
48 }
49 /* { dg-output "PASS.*" } */