cond.c revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s 2// CHECK-NOT: WARNING: ThreadSanitizer: data race 3// CHECK-NOT: ThreadSanitizer WARNING: double lock 4// CHECK-NOT: ThreadSanitizer WARNING: mutex unlock by another thread 5// CHECK: OK 6 7#include <stdio.h> 8#include <stdlib.h> 9#include <pthread.h> 10 11pthread_mutex_t m; 12pthread_cond_t c; 13int x; 14 15void *thr1(void *p) { 16 int i; 17 18 for (i = 0; i < 10; i += 2) { 19 pthread_mutex_lock(&m); 20 while (x != i) 21 pthread_cond_wait(&c, &m); 22 x = i + 1; 23 pthread_cond_signal(&c); 24 pthread_mutex_unlock(&m); 25 } 26 return 0; 27} 28 29void *thr2(void *p) { 30 int i; 31 32 for (i = 1; i < 10; i += 2) { 33 pthread_mutex_lock(&m); 34 while (x != i) 35 pthread_cond_wait(&c, &m); 36 x = i + 1; 37 pthread_mutex_unlock(&m); 38 pthread_cond_broadcast(&c); 39 } 40 return 0; 41} 42 43int main() { 44 pthread_t th1, th2; 45 46 pthread_mutex_init(&m, 0); 47 pthread_cond_init(&c, 0); 48 pthread_create(&th1, 0, thr1, 0); 49 pthread_create(&th2, 0, thr2, 0); 50 pthread_join(th1, 0); 51 pthread_join(th2, 0); 52 fprintf(stderr, "OK\n"); 53} 54