[concurrency-interest] Min and Max for Atomics

Nathan and Ila Reynolds nathanila at gmail.com
Thu Aug 10 14:31:11 EDT 2017


Hmm... you raise a good point for VarHandle.  I speak from a position of 
knowing very little about VarHandle.  Perhaps, this logic is better in a 
utility class which accepts a VarHandle and the value.

-Nathan

On 8/10/2017 12:18 PM, Brian S O'Neill wrote:
> 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
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

-- 
-Nathan



More information about the Concurrency-interest mailing list