[concurrency-interest] Re : Lock-free implementation of min including assignment

Markus Jevring m.jevring at iontrading.com
Fri Jan 20 03:48:37 EST 2012


These seem like they would be equivalent to what I posted after having 
applied the optimizations Martin mentioned.
Given that, I would say we're converging on a correct solution =)

On 19/01/2012 23:31, Jeff Hain wrote:
> Hello.
>
> I recently posted some code in this list, containing such (correct I 
> think!) implementations.
>
> Now that I read them again, I realize my Javadoc is misleading, since 
> it says there is (always)
> an atomic set done, while it is only done if needed.
>
> Here they are:
>
>     /**
>      * Atomically sets the specified atomic long with min(atomic long 
> value, specified value),
>      * and returns the new atomic long value (which might have not 
> changed).
>      */
>     private static long setMinAndGet(AtomicLong atomic, long value) {
>         long tmpLastReturned;
>         do {
>             tmpLastReturned = atomic.get();
>             if (tmpLastReturned <= value) {
>                 return tmpLastReturned;
>             }
>             // Here, value < tmpLastReturned,
>             // so we will try to set it as new value.
>         } while (!atomic.compareAndSet(tmpLastReturned, value));
>         return value;
>     }
>
>     /**
>      * Atomically sets the specified atomic long with max(atomic long 
> value, specified value),
>      * and returns the new atomic long value (which might have not 
> changed).
>      */
>     private static long setMaxAndGet(AtomicLong atomic, long value) {
>         long tmpLastReturned;
>         do {
>             tmpLastReturned = atomic.get();
>             if (tmpLastReturned >= value) {
>                 return tmpLastReturned;
>             }
>             // Here, value > tmpLastReturned,
>             // so we will try to set it as new value.
>         } while (!atomic.compareAndSet(tmpLastReturned, value));
>         return value;
>     }
>
> -Jeff
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120120/2a3e7685/attachment.html>


More information about the Concurrency-interest mailing list