[concurrency-interest] Composite compare-and-swap based on two values

Daniel Yokomizo daniel.yokomizo at gmail.com
Tue Sep 6 06:39:54 EDT 2011


java.util.concurrent.atomic.AtomicStampedReference<V> may be relevant to
your problem.

On Sep 6, 2011 7:26 AM, "Oleksiy Khilkevich" <oleksiy.khilkevich at gmail.com>
wrote:
>
> One way came to my mind right now, is there possibility to introduce
versioning here?
> Say AtomicInteger version is incremented each time, the counter state is
changed, whether it is boundary change or value change. And instead of
checking value and boundary I'll just check if the version was changed?
>
> Looks like a possibility to me, but please judge my thinking :)
>
> Thanks
>
> 2011/9/6 Oleksiy Khilkevich <oleksiy.khilkevich at gmail.com>
>>
>> Hi Concurrency Champs
>>
>> I'm implementing non-blocking cyclic counter mostly for learning
purposes, and faced the following problem, which i'm not sure how to solve.
>>
>> The method in question implements the following
>>
>>     /**
>>      * Sets counter value to initial (boundary) value
>>      * @return true if counter value was changed
>>      */
>>     boolean reset();
>>
>> The implementation I'm not sure about
>>
>>     @Override
>>     public boolean reset() {
>>         for (;;) {
>>             int curValue = value.get();
>>             int curBoundary = boundary.get();
>>             if (curValue == curBoundary) return false;
>>
>>             // TODO: if boundary and value were not changed, set the
value to boundary
>>
>>             return true;
>>         }
>>     }
>>
>> Here I have two values which are both AtomicIntegers and which may change
- value can be incremented or decremented by another thread, and boundary
can be changed too.
>>
>> I'm not quite sure how to do this without introducing critical sections.
In general the question refers to any shared state change based on values of
several other shared states.
>>
>> What is the correct way to implement such operation atomically without
critical sections? IS it possible to build composite CAS operations based on
AtomicInteger#compareAndSet ?
>>
>> Thank you and kindly waiting for your ideas
>> Oleksiy
>>
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20110906/13df3537/attachment.html>


More information about the Concurrency-interest mailing list