mutexset7.cc revision 6a211c5814e25d6745a5058cc0e499e5235d3821
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s 2#include <pthread.h> 3#include <stdio.h> 4#include <unistd.h> 5 6int Global; 7__thread int huge[1024*1024]; 8 9void *Thread1(void *x) { 10 sleep(1); 11 Global++; 12 return NULL; 13} 14 15void *Thread2(void *x) { 16 pthread_mutex_t *mtx = new pthread_mutex_t; 17 pthread_mutex_init(mtx, 0); 18 pthread_mutex_lock(mtx); 19 Global--; 20 pthread_mutex_unlock(mtx); 21 pthread_mutex_destroy(mtx); 22 delete mtx; 23 return NULL; 24} 25 26int main() { 27 pthread_t t[2]; 28 pthread_create(&t[0], NULL, Thread1, NULL); 29 pthread_create(&t[1], NULL, Thread2, NULL); 30 pthread_join(t[0], NULL); 31 pthread_join(t[1], NULL); 32} 33 34// CHECK: WARNING: ThreadSanitizer: data race 35// CHECK: Write of size 4 at {{.*}} by thread T1: 36// CHECK: Previous write of size 4 at {{.*}} by thread T2 37// CHECK: (mutexes: write [[M1:M[0-9]+]]): 38// CHECK: Mutex [[M1]] is already destroyed 39// CHECK-NOT: Mutex {{.*}} created at 40 41