On 01/10/15 12:28 +0100, Andrew Haley wrote:
On 09/29/2015 04:21 PM, Sandra Loosemore wrote:
What is "weak compare_exchange", and what is "the strong variation", and
how do they differ in terms of behavior?
It's in C++11 29.6.5:
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., load- locked store-conditional
machines. A consequence of spurious failure is that nearly all uses of
weak compare-and-exchange will be in a loop. When a
compare-and-exchange is in a loop, the weak version will yield better
performance on some platforms. When a weak compare-and-exchange would
require a loop and a strong one would not, the strong one is
preferable. — end note ]
The classic use of this is for shared counters: you don't care if you
miss an occasional count but you don't want the counter to go
backwards.
Whether we should replicate all of the C++11 language is perhaps
something we should discuss.
I would suggest we don't try to reproduce the standard definition, but
just say the weak version can fail spuriously and the strong can't.
IMHO this isn't the place to educate people in the fine points of
low-level atomics. As it says, "when in doubt use the strong
variation".
i.e. apply this in addition to my earlier suggestion.
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 0de94f2..ce1b4ae 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -9354,7 +9354,8 @@ This compares the contents of @code{*@var{ptr}} with the contents of
operation that writes @var{desired} into @code{*@var{ptr}}. If they are not
equal, the operation is a @emph{read} and the current contents of
@code{*@var{ptr}} are written into @code{*@var{expected}}. @var{weak} is true
-for weak compare_exchange, and false for the strong variation. Many targets
+for weak compare_exchange, which may fail spuriously, and false for
+the strong variation, which never fails spuriously. Many targets
only offer the strong variation and ignore the parameter. When in doubt, use
the strong variation.