1 /* { dg-do run } */ 2 /* { dg-options "-pthread" } */ 3 /* { dg-require-effective-target pthread } */ 4 5 #include <pthread.h> 6 7 static volatile int sem1; 8 static volatile int sem2; 9 f(void * va)10static void *f(void *va) 11 { 12 void **p = va; 13 if (*p) return *p; 14 sem1 = 1; 15 while (!sem2); 16 __atomic_thread_fence(__ATOMIC_ACQUIRE); 17 // GCC used to RTL-CSE this and the first load, causing 0 to be returned 18 return *p; 19 } 20 main()21int main() 22 { 23 void *p = 0; 24 pthread_t thr; 25 if (pthread_create(&thr, 0, f, &p)) 26 return 2; 27 while (!sem1); 28 __atomic_thread_fence(__ATOMIC_ACQUIRE); 29 p = &p; 30 __atomic_thread_fence(__ATOMIC_RELEASE); 31 sem2 = 1; 32 pthread_join(thr, &p); 33 return !p; 34 } 35