[cpp-threads] Weak vs Strong Atomics

Roger Orr rogero at howzatt.demon.co.uk
Thu May 3 20:01:16 BST 2007


 
> First, I don't think the notion that you can pass a weak
> atomic to a module expecting a strong atomic actually works.
> The passee may be expecting SC semantics, but the passer may
> still be using it weakly, thus failing SC.  Conversely, as
> noted, passing a strong atomic to a weak atomic also may
> yield non-SC behavior to the passer.  So, at a minimum,
> meeting the stated goal of the decision requires complete
> isolation of the weak atomics from the strong atomics, and
> that was not my understanding of the decision.

That matches my understanding that we were proposing a class hierarchy,
where weak_atomic was derived from strong_atomic, as we thought
a weak atomic IS-A strong atomic.

I think your first sentence shows that assumption is invalid.

> Second, I don't think atomic variables are likely to be
> shared among disparate code.

Agreed.

> 
> Based on these two observations, I would like to keep the
> current design for atomics, which is a single atomic type and
> a functional interface to all operations and an operator
> interface to the strong operations.
>
> Comments?

I think I'd prefer something like this:

- a C-style atomic type (usable in both C and C++ with the same semantics in
both)
- free functions that operate on the C-style type, providing both strong and
weak operations

- a C++ (SC) atomic class
- member functions of the atomic class (not yet sure whether they should be
operators or named members)
- some explicit conversion between the C++ class and the C type

I have some concern over the current atomic types proposal in that the C and
C++ types have different semantics.

// C
{
   atomic_int a;
   atomic_int b;
   a = b;
}

// C++
{
  std::atomic_int a;
  std::atomic_int b;
  a = b; // Error, as op= declared with "= delete" in C++ definition (using
N2210)
}

So could we:
- declare struct atomic_int as a pretty nearly raw struct in both C and C++
(in std:: here)
- move all the C++ 'clever stuff' into std::atomic<int> and have that as the
strong atomic class
- add an explicit conversion operator/accessor to atomic<int> to get access
to the underlying atomic_int type.

Regards,
Roger.





More information about the cpp-threads mailing list