Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid infinite loop when counter is incremented from multiple threads #293

Closed
wants to merge 1 commit into from
Closed

Conversation

tom-code
Copy link

@tom-code tom-code commented Aug 7, 2019

It seems that gauge/counter increment could cause infinite loop in case it is changed from different thread betwen value load and compare_exchange_weak operation.
When compare_exchange_weak fails new value must to be read to be able to succeed at some point.

@tom-code tom-code closed this Aug 28, 2019
@gjasny
Copy link
Collaborator

gjasny commented Sep 1, 2019

From std::atomic<T>::compare_exchange_weak( T& expected, T desired , ...):

Atomically compares the object representation (until C++20) value representation (since C++20) of *this with that of expected, and if those are bitwise-equal, replaces the former with desired (performs read-modify-write operation). Otherwise, loads the actual value stored in *this into expected (performs load operation).

So expected is updated if modification fails

gjasny added a commit that referenced this pull request Nov 12, 2021
gjasny added a commit that referenced this pull request Nov 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants