[concurrency-interest] Min and Max for Atomics

Brian S O'Neill bronee at gmail.com
Thu Aug 10 14:18:23 EDT 2017


Why should these functions be built into the VarHandle class? Are there 
any machine-specific instructions that can be targeted? If the 
implementation of these methods would always match your suggestion, then 
I don't see any reason why the VarHandle class should have them. 
Complicating further would be all the various permutations. Should this 
use acquire/release semantics? Weak? Volatile? Opaque?

On 2017-08-10 04:51 AM, Nathan and Ila Reynolds wrote:
> Have the following methods been considered to be added to their 
> respective classes?  Perhaps, there are other Atomic classes where these 
> should be added.  Perhaps, VarHandles should have these added (assume I 
> know nothing about VarHandles).
> 
> The advantage of these methods is that they avoid cache invalidation and 
> a fence if there is no update.  This is kind of related to the previous 
> discussion about getAndUpdate() and updateAndGet().
> 
> I am proposing these methods since I have written this code a few 
> times.  Until these methods are implemented in the JDK, perhaps I should 
> write an atomic utility class.
> 
> AtomicInteger
> 
> public int max(int value)
> {
>     int expect;
> 
>     while (true)
>     {
>        expect = get();
> 
>        if (expect >= value)
>           return expect;
> 
>        if (compareAndSet(expect, value))
>           return expect;
>     }
> }
> 
> public int min(int value);
> {
>     int expect;
> 
>     while (true)
>     {
>        expect = get();
> 
>        if (expect <= value)
>           return expect;
> 
>        if (compareAndSet(expect, value))
>           return expect;
>     }
> }
> 
> AtomicLong
> public long max(long value);
> public long min(long value);
> 
> 
> -- -Nathan
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest


More information about the Concurrency-interest mailing list