[concurrency-interest] increment()/decrement() methods on atomicInteger ?

Brian Goetz brian at quiotix.com
Mon May 30 14:49:00 EDT 2005


  In some cases it is desirable to increment/decrement an atomic integer,
> without getting the result (for example- in a set implementation when 
> you add/remove an element (updating an atomic "size" which is just a 
> "recommendation").  Would'nt it be helpful to add
> 
> increment/decrement methods, which donot return a value ?  Assuming  
> that an atomic increment can be delegated to the
> processor level (at least for volatile) - this can save the CAS retry cycle

This does not save the CAS retry cycle.  Increment looks like this:

while (true) {
   int i = get();
   if (cas(i, i+1))
     return i+1;
}

To implement a non-value-returning increment, you'd still have to do this:

while (true) {
   int i = get();
   if (cas(i, i+1))
     break;
}

So there's no performance advantage to having a non-value-returning 
increment operation.  I agree it might save you a little typing, but 
that's not really a great reason to add a method (and three increment 
methods might be more confusing than two.)



More information about the Concurrency-interest mailing list