I have a background thread which loops on a state variable done
. When I want to stop the thread I set the variable done
to true
. But apparently this variable is never set. I understand that the compiler might optimize it away so I have marked done
volatile
. But that seems not to have any effect. Note, I am not worried about race conditions so I have not made it atomic
or used any synchronization constructs. How do I get the thread to not skip testing the variable at every iteration? Or is the problem something else entirely? done
is initially false
.
struct SomeObject
{
volatile bool done_;
void DoRun();
};
static void RunLoop(void* arg)
{
if (!arg)
return;
SomeObject* thiz = static_cast<SomeObject*>(arg);
while( !(thiz->done_) ) {
thiz->DoRun();
}
return;
}