1#include <pthread.h> 2#include <semaphore.h> 3#include <stdio.h> 4 5struct 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 19struct B : A { 20 virtual void F() { 21 } 22 virtual ~B() { 23 sem_wait(&sem_); 24 sem_destroy(&sem_); 25 } 26}; 27 28static A *obj = new B; 29 30void *Thread1(void *x) { 31 obj->F(); 32 obj->Done(); 33 return NULL; 34} 35 36void *Thread2(void *x) { 37 delete obj; 38 return NULL; 39} 40 41int 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// CHECK: PASS 50// CHECK-NOT: WARNING: ThreadSanitizer: data race 51