vptr_harmful_race.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1// RUN: %clangxx_tsan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s 2#include <pthread.h> 3#include <semaphore.h> 4#include <stdio.h> 5#include <unistd.h> 6 7struct A { 8 A() { 9 sem_init(&sem_, 0, 0); 10 } 11 virtual void F() { 12 } 13 void Done() { 14 sem_post(&sem_); 15 } 16 virtual ~A() { 17 sem_wait(&sem_); 18 sem_destroy(&sem_); 19 } 20 sem_t sem_; 21}; 22 23struct B : A { 24 virtual void F() { 25 } 26 virtual ~B() { } 27}; 28 29static A *obj = new B; 30 31void *Thread1(void *x) { 32 obj->F(); 33 obj->Done(); 34 return NULL; 35} 36 37void *Thread2(void *x) { 38 sleep(1); 39 delete obj; 40 return NULL; 41} 42 43int main() { 44 pthread_t t[2]; 45 pthread_create(&t[0], NULL, Thread1, NULL); 46 pthread_create(&t[1], NULL, Thread2, NULL); 47 pthread_join(t[0], NULL); 48 pthread_join(t[1], NULL); 49} 50 51// CHECK: WARNING: ThreadSanitizer: data race on vptr 52