std::atomic<bool> stores a Boolean value and can be initialized or assigned from a plain bool. Assignment returns the stored bool, not a reference to the atomic object. section 5.2.3
Its main operations cover querying, writing, and conditional updates:
- Read the value with load() or an implicit conversion to bool. section 5.2.3
- Write true or false with store(); replace the value and retrieve its previous value with exchange(). section 5.2.3
- Use compare-exchange to store a new value only when the current value equals an expected value. These operations can take memory-order arguments. section 5.2.3
Conditionally set a flag
The loop attempts to change b to true while compare_exchange_weak fails and expected remains false. section 5.2.3
std::atomic<bool> b; bool x=b.load(std::memory_order_acquire); b.store(true); x=b.exchange(false,std::memory_order_acq_rel);
See load, store, and exchange used with a Boolean atomic, including explicit memory-order arguments. section 5.2.3
