Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

compare_exchange

31 views
Skip to first unread message

Doug Mika

unread,
Aug 12, 2015, 10:25:19 PM8/12/15
to
So the compare_exchange_weak is defined as follows:
bool compare_exchange_weak( T& expected, T desired,
std::memory_order success,
std::memory_order failure );
where it atomically compares *this == expected and
*this==expected ==> *this=desired;
*this!=expected ==> expected=*this.

My question is, I can make sense of the condition evaluating to true and why we would want it, but why would we want expected=*this when we get false? Where is this used (How did they get the idea to let expected=*this if *this!=expected?)

Lőrinczy Zsigmond

unread,
Aug 13, 2015, 7:20:53 AM8/13/15
to
I think you are trying to ask about compare-and-swap mechanism,
which is an important tool in synchronization.

https://en.wikipedia.org/wiki/Compare-and-swap

A simpler version of this is test-and-set:

https://en.wikipedia.org/wiki/Test-and-set

Bo Persson

unread,
Aug 13, 2015, 11:38:17 AM8/13/15
to
The idea is to detect if 'expected' is unchanged since last time you
read it, and then update its value. If not, you might want to know what
has happened - like, has someone else changed the value? Into what?


Bo Persson

0 new messages