Reputation: 15
I'm new to concurrent programming and I'm trying to compile the following code:
private:
std::atomic<bool> resizing_;
void Resize() { if (resizing_.compare_exchange_strong(false, true)... }
This throws error: no matching member function for call to 'compare_exchange_strong'
and I'm not sure how I can fix this. I've tried using a bool*
for the first argument but that didn't seem to help. I've tried to read the documentation on atomic<>
s but it hasn't helped.
Any information on what I'm doing wrong would be really helpful!
Upvotes: 0
Views: 4466
Reputation: 6584
The first parameter of the compare_exchange_strong
method is a reference to the type. This method exchanges two values, but only if the comparison of the contained
value and the expected
value is true. Otherwise it replaces the expected with the contained value.
The idiom is like this:
std::atomic<int> value;
int expected = value;
do {
int new_value = get_updated_value(expected);
} while(!value.compare_exchange_strong(expected, new_value));
Note that the expected
is automatically updated whenever the comparison is false, and another new_value
is evaluated each iteration.
Upvotes: 3