Condition for multiple std :: atomic <T>

In my multi-threaded application, I have a condition that can be reduced to this example

std::atomic<bool> a, b; // ... if ( a.load() && b.load() ) { // ... } 

Obviously, immediately after condition a and b may contain different values.

In my application, he claims that if both values ​​are true at the same time, they can no longer change state. But after returning a.load() true it can change its value even before b.load() is evaluated.

Is there an elegant solution for atomic evaluation of this statement? Obviously, blocking each call to a.store (..) and b.store (..) will work here, but this is far from pleasant.

+4
source share
1 answer

Have you considered using atomic<int> instead and setting bits for bools? This will allow you to query as atomically.

+5
source

All Articles