Is it safe to have a variable passed by const reference modified externally (via an interrupt)?
std::atomic_bool g(true);
void sig_handler(int num) {
switch(num) {
case SIGHUP:
g = false;
break;
}
}
void method(const std::atomic_bool &flag) {
while(flag) {
...
}
}
method(g); // blocks
Here we have a const reference that is modified externally via an interrupt. I get the fact that the callee (method
) can't modify flag
, but can the caller modify g
? Because in g
space std::atomic_bool
is not const.