Subject: Re: [boost] Notice: Boost.Atomic (atomic operations library)
From: Helge Bahmann (hcb_at_[hidden])
Date: 2009-12-05 12:52:38
On Fri, 4 Dec 2009, Phil Endecott wrote:
> Helge Bahmann wrote:
>> there is a pseudo-code explanation in the boost::atomic class documentation
> Ah sorry, I didn't realise that compare_exchange_* were in the external
> interface; for some reason I thought they were internal.
they are external, but I have now changed the implementation that backends
must implement "four-operand" compare_exchange_*, which specifies two
memory_order operands (one for successful exchange, one for failed), but
give it a day until it is committed into the public repo
the three-operand version is then provided by the front-end
> OK, so since my kernel_cmpxchg() function doesn't return the old value but
> only a flag, I need something like:
> bool success = kernel_cmpxchg(e,d,&i);
> if (!success) e = i;
> But i may have changed again between the kernel_cmpxchg() and that
> assignment. Is that OK?
that's okay -- there are two cases:
- "new" value (loaded after failed cmpxchg) does not match "expected",
then the caller receive a more up-to-date value and retry with this one,
after all it does not matter whether it failed with the "old" or "new"
- "new" value (loaded after failed cmpxchg) matches "expected":
compare_exchange_weak is allowed to fail "spuriously"
> Should I use load() there? If so, what memory order is needed?
the load is internal, so load it "relaxed" , you only have to maintain
the memory order for the "total" cmpxchg, successful or failed
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk