volatile and multithreading?

前端 未结 6 717
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 22:23

In the following code:

#include 
#include 
#include 

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int          


        
6条回答
  •  执念已碎
    2020-12-06 23:22

    Volatile is needed here in the presence of optimization. Otherwise the read of ready can be legally moved out of the while loop.

    Assuming limitations to optimization that are not promised by the standard may be fine now, but cause great grief to future maintainers as compilers improve.

提交回复
热议问题