1#include <pthread.h> 2#include <stdlib.h> 3#include <stdio.h> 4#include <sched.h> 5 6struct Cache { 7 int x; 8}; 9 10Cache g_cache; 11 12Cache *CreateCache() { 13 g_cache.x = rand(); 14 return &g_cache; 15} 16 17_Atomic(Cache*) queue; 18 19void *Thread1(void *x) { 20 static Cache *c = CreateCache(); 21 __c11_atomic_store(&queue, c, 0); 22 return 0; 23} 24 25void *Thread2(void *x) { 26 Cache *c = 0; 27 for (;;) { 28 c = __c11_atomic_load(&queue, 0); 29 if (c) 30 break; 31 sched_yield(); 32 } 33 if (c->x >= RAND_MAX) 34 exit(1); 35 return 0; 36} 37 38int main() { 39 pthread_t t[2]; 40 pthread_create(&t[0], 0, Thread1, 0); 41 pthread_create(&t[1], 0, Thread2, 0); 42 pthread_join(t[0], 0); 43 pthread_join(t[1], 0); 44} 45 46// CHECK: WARNING: ThreadSanitizer: data race 47