Is volatile bool for thread control considered wrong?

volatile can be used for such purposes. However this is an extension to standard C++ by Microsoft:

Microsoft Specific

Objects declared as volatile are (…)

  • A write to a volatile object (volatile write) has Release semantics; (…)
  • A read of a volatile object (volatile read) has Acquire semantics; (…)

This allows volatile objects to be used for memory locks and releases in multithreaded applications.(emph. added)

That is, as far as I understand, when you use the Visual C++ compiler, a volatile bool is for most practical purposes an atomic<bool>.

It should be noted that newer VS versions add a /volatile switch that controls this behavior, so this only holds if /volatile:ms is active.

Leave a Comment