I'm unsure if it's me not understanding or the documentation isn't clearly formulated.
The following excerpt has been taken from the newest draft (N3126, section 29.6):
bool atomic_compare_exchange_weak(volatile A* object, C * expected, C desired);
bool atomic_compare_exchange_weak(A* object, C * expected, C desired);
bool atomic_compare_exchange_strong(volatile A* object, C * expected, C desired);
bool atomic_compare_exchange_strong(A* object, C * expected, C desired);
bool atomic_compare_exchange_weak_explicit(volatile A* object, C * expected, C desired, memory_order success, memory_order failure);
bool atomic_compare_exchange_weak_explicit(A* object, C * expected, C desired, memory_order success, memory_order failure);
bool atomic_compare_exchange_strong_explicit(volatile A* object, C * expected, C desired, memory_order success, memory_order failure);
bool atomic_compare_exchange_strong_explicit(A* object, C * expected, C desired, memory_order success, memory_order failure);
bool A::compare_exchange_weak(C & expected, C desired, memory_order success, memory_order failure) volatile;
bool A::compare_exchange_weak(C & expected, C desired, memory_order success, memory_order failure);
bool A::compare_exchange_strong(C & expected, C desired, memory_order success, memory_order failure) volatile;
bool A::compare_exchange_strong(C & expected, C desired, memory_order success, memory_order failure);
bool A::compare_exchange_weak(C & expected, C desired, memory_order order = memory_order_seq_cst) volatile;
bool A::compare_exchange_weak(C & expected, C desired, memory_order order = memory_order_seq_cst);
bool A::compare_exchange_strong(C & expected, C desired, memory_order order = memory_order_seq_cst) volatile;
bool A::compare_exchange_strong(C & expected, C desired, memory_order order = memory_order_seq_cst);
Remark: The weak compare-and-exchange
operations may fail spuriously, that
is, return false while leaving the
contents of memory pointed to by
expected before the operation is the
same that same as that of the object
and the same as that of expected after
the operation. [ Note: This spurious
failure enables implementation of
compare-and-exchange on a broader
class of machines, e.g., loadlocked
store-conditional machines. A
consequence of spurious failure is
that nearly all uses of weak
compare-and-exchange will be in a
loop.
So, what does this mean?
Firstly, it 'may' fail spuriously?! Why would it fail? And how do they define 'may'?
Secondly, I still have no idea what's the difference between the functions with "_strong" and "_weak" suffix. Could someone explain the difference?
EDIT:
That's what I've found in libstdc++-implementation (atomic_0.h):
bool compare_exchange_weak(
__integral_type& __i1,
__integral_type __i2,
memory_order __m1,
memory_order __m2
)
{
__glibcxx_assert(__m2 != memory_order_release);
__glibcxx_assert(__m2 != memory_order_acq_rel);
__glibcxx_assert(__m2 <= __m1);
return _ATOMIC_CMPEXCHNG_(this, &__i1, __i2, __m1);
}
bool compare_exchange_strong(
__integral_type& __i1,
__integral_type __i2,
memory_order __m1,
memory_order __m2
)
{
__glibcxx_assert(__m2 != memory_order_release);
__glibcxx_assert(__m2 != memory_order_acq_rel);
__glibcxx_assert(__m2 <= __m1);
return _ATOMIC_CMPEXCHNG_(this, &__i1, __i2, __m1);
}
question from:
https://stackoverflow.com/questions/4944771/stdatomic-compare-exchange-weak-vs-compare-exchange-strong