#include #include #include unsigned long long global = 0x00010000; #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x)) void *thread_set_function(void *arg); void *thread_read_function(void *arg); int main(int argc, char **argv) { int ret; pthread_t pthread_set; pthread_t pthread_read; ret = pthread_create(&pthread_set, NULL, thread_set_function, NULL); if (ret) { fprintf(stderr, "ERROR: fail to create set_thread\n"); exit(EXIT_FAILURE); } ret = pthread_create(&pthread_read, NULL, thread_read_function, NULL); if (ret) { fprintf(stderr, "ERROR: fail to create read_thread\n"); exit(EXIT_FAILURE); } pthread_join(pthread_set, NULL); pthread_join(pthread_read, NULL); return 0; } void *thread_set_function(void *arg) { sleep(1); while(1) { ACCESS_ONCE(global) = 0x0000000000000100; ACCESS_ONCE(global) = 0x0010000000000000; } } void *thread_read_function(void *arg) { if (global) printf("no 0 \n"); while(1) { if (global == 0) { printf("0 is detected\n"); exit(0); } } }