volatile and multithreading?

前端 未结 6 720
被撕碎了的回忆
被撕碎了的回忆 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:20

    yes, volatile int ready = 0; is always needed here.

    Update
    If you would like no optimization around some code snippet, you can either use #pragma directives around that code (if your compiler supports them) or - which is totally portable - move the code to separate files and compile these files with no or little optimizations.
    The latter approach might still require usage of the volatile keyword, since the ready variable might be used by other modules which you might want to optimize.

提交回复
热议问题