-
Notifications
You must be signed in to change notification settings - Fork 264
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
the Atomics page left me confused about "relaxed". What is it for? #309
Comments
I'm no atomics expert. But I think the idea is that it will be correct, eventually. You might have to wait some time for it to settle first, but it'll get there. |
I'd assume that, but I can't see any way of using it without some guarantee about when it will have settled. Even if it were just "guaranteed to become readable after 2 milliseconds" that would be really good to know, I would probably be able to think of some uses for that, but with this few guarantees it makes me confused about what atomicity even is at that point. I think the source of the confusion might be "They can be freely re-ordered and provide no happens-before relationship". It looks like they do provide a happens-before relationship, as long as you use |
The linked cppreference page has a good usecase example for it:
Incrementing counters would still require atomiticity, i.e. if two threads try to increment you want to make sure that the thread will increment only after the other thread is done so neither of the increment operation is accidentally swallowed in data race--it would be disastrous in the case of the reference counters of shared_ptr (equivalent with Arc in Rust), as it may cause UAF. But it does not require ordering, i.e. it doesn't matter which thread gets to increment first. (I highly recommend that cppreference page for trying to understand the different memory ordering schemes, by the way) |
I do not see from reading that why it's safe to use I notice that the CPPReference mentions that relaxed writes will be synchronized if the other thread does a |
There are no memory accesses that need to be synchronized by The Also, although atomic orderings don't dictate when a sideeffect will be visible to another thread, there's a single modification order on each atomic object, and the |
I'm also confused by this "relaxed" |
Could you explain this sentence better? what does the sequenced-before mean? |
Atomics
The example of a counter is given, the counter would keep an accurate count, but no guarantee is given that another thread reading the counter will get the final, up to date count?
If there's no guarantee that the count being read is correct, from the perspective of a reader, why ever read from it? Why bother with it at all?
The text was updated successfully, but these errors were encountered: