condition on several std::atomic<T>

atomic, c++, c++11, multithreading

Solution

Have you considered using an `atomic<int>` instead, and setting bits for the bools? This would allow you to query both atomically.

Problem

In my multithreaded application I have a condition that can be reduced to this example ``` std::atomic<bool> a, b; // ... if ( a.load() && b.load() ) { // ... } ``` Obviously, directly after the condition, a and b can hold different values. In my application it holds that, if both values are true simultaneously, they cannot change state ever again. But after `a.load()` returned `true` it might change its value even before `b.load()` is evaluated. Is there an elegant solution for atomically evaluating this statement? Obviously locking every call of a.store(..) and b.store(..) would work here, but that's far from nice.

Original source