I have read many questions about this topic here, but still feel like the point is being missed by many answers I read.
The Question: Should variables shared between threads in pure C be marked volatile
?
Disclaimer: I very much understand that volatile
does not mean atomic. That is, just because my variables are marked volatile
doesn’t mean I don’t need to worry about race conditions.
The Point: If I have a variable A
which is made atomic via the use of a mutex, shouldn’t A
still be marked volatile
to prevent the compiler from optimizing out reads to A
?
Consider the following example:
mutex_t m;
static int flag = 0;
// Thread 1
void writer(void) {
lock(m)
flag = 1;
unlock(m);
}
// Thread 2
void reader(void) {
int read = 0;
while (1) {
lock(m);
read = flag;
unlock(m);
if (read) exit(0);
}
}
Given the functions writer
and reader
are executing in different threads, wouldn’t it be possible for the compiler to optimize out the repeated reads to flag
inside the reader
function?
I feel like flag here should be made volatile
?
Final Disclaimer: I understand that there probably exists a nice atomic/thread-safe type which could be used for small integral values like flag
in this example. However, I am more asking this question for the situation where the data shared between two threads is a large struct.