[cpp-threads] Re: Increment/decrement operators on atomics package

Boehm, Hans hans.boehm at hp.com
Tue Apr 24 00:39:33 BST 2007


> -----Original Message-----
> From:  Lawrence Crowl
> Sent: Friday, April 20, 2007 5:45 AM
> To: Raul Silvera
> Cc: C++ threads standardisation
> Subject: [cpp-threads] Re: Increment/decrement operators on 
> atomics package
> 
> On 4/20/07, Raul Silvera <rauls at ca.ibm.com> wrote:
> >
> > Lawrence, something that we briefly touched on in Oxford was the 
> > question of increment operators in the atomics package 
> doing a RMW operation.
> >
> > I think that it is in generally confusing for t+=1 and 
> t=t+1 to have 
> > different semantics for an Atomic t. The difference would 
> be that t+=1 
> > would be RMW operation while t=t+1 is a sequence of 
> > load_acquire;add;store_release.
> > The main concern is that users consider these two idioms 
> > interchangeable and replace one by the other only based on 
> programming style.
> 
> Note that this transformation is not semantically neutral in 
> the absence of atomics.  f()+=1 is not equivalent to f()=f()+1.
> 
> > My proposal for this issue is to make t+=1 have the same 
> semantics as 
> > t=t+1 (no RMW). Instead, a separate member functions would 
> be provided 
> > to do RMW operations (e.g t.atomic_inc(1)).
> > An alternative proposal would be not to define increment 
> operators for 
> > the atomic class.
> >
> > Do you consider this a valid concern? Thanks...
> 
> I consider it a valid concern, .  The choice, I think is 
> between the notational convenience of += and the education of 
> the users.  Given that using atomic will take substantial 
> education anyway, I fell on the side of notational 
> convenience.  If there is a consensus to remove the operator, 
> I think that the same reasoning applies to ++ operators.
> 
I actually have no idea which one would be more error prone for uneducated users.  My impression (backed up by very limited experience) is that the more naïve users may be surprised if you tell them that any of the above, even t = t + 1, is NOT atomic.  Thus my inclination would be to follow the rule that any operations on atomics that reasonably could be atomic, should be.

Hans



More information about the cpp-threads mailing list