Please note: This is just a question out of curiosity, but not about writing better-multithreaded code. I don't and won't write code like this in real projects, of course.
Inline substitution may occur when the inline keyword is added.
So I'm curious.
Let's say we have code like this:
static bool done = false;
inline void setDone(bool newState) {
done = newState;
}
inline bool getDone() {
return done;
}
void someWorkerThreadJob() {
// Accessing without any lock/atomic/volatile
while (getDone() == false) {
}
}
Can someWorkerThreadJob() be compiled like below and run into an infinite loop?
void someThreadJob() {
while (done == false) {
}
}
This also leads me to the next question.
What about the getters and setters in classes?
Member functions defined inside a class are implicitly inline, so I think inline substitution can occur, thus the same problem.
Is this correct?