It is probably sort of thread-safe.
Thread safety tends to depend on context. Updating a bool is always thread safe, if you never read from it.
And if you do read from it, then the answer depends on when you read from it, and what that read signifies.
On some CPUs, but not all, writes to an object of type bool
will be atomic. x86 CPUs will generally make it atomic, but others might not. If the update isn't atomic, then adding volatile
won't help you.
But the next problem is reordering. The compiler (and CPU) will carry out reads/writes to volatile
variables in the order specified, without any reordering. So that's good.
But it makes no guarantee about reordering one volatile
memory access relative to all the non-volatile
ones. So a common example is that you define some kind of flag to protect access to a resource, you make the flag volatile
, and then the compiler moves the resource access up so it happens before you check the flag. It's allowed to do that, because it's not reordering the internal ordering of two volatile
accesses, but merely a volatile
and a non-volatile
one.
Honestly, the question I'd ask is why not just do it properly?
It is possible that volatile
will work in this situation, but why not save yourself the trouble, and make it clearer that it's correct? Slap a memory barrier around it instead.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…