Volatile Variables in Optimized C
Ah, forgetting the volatile type qualifier when compiling with optimization enabled:12345678910111213141516171819// compile with: gcc -O3 -lpthread volatile.c -o volatile// Fix by using volatile:// volatile int flag = 0;int flag = 0;void *update() { flag = 1;}int main() { pthread_t update_thread; pthread_create(&update_thread, NULL, update, NULL); while (!flag) {} // This will loop forever pthread_join(update_thread, NULL);}