Ah, well in that case, one must consider that the delta is not meaningful
in the long term - it might change immediately after the call. So you
could get away with something like this:
public class CasExample {
private final AtomicInteger val = new AtomicInteger(0);
public int getAndSetDelta(int expect, int update) {
if (val.compareAndSet(expect, update)) {
return 0;
} else {
return val.get() - expect;
}
}
}
Since the value might change at any time, you can't prove that it wasn't
atomic. You could also add in a check - if the CAS failed and the returned
delta would be zero, then retry.
- DML
On 02/10/2010 09:49 AM, Brian Stansberry wrote:
You're mixing the use of the 'expect' param vs ths
'update' param. The
delta he's talking about would be on the 'expect' vs current value.
On 02/10/2010 09:40 AM, David M. Lloyd wrote:
> This doesn't really make any sense - you're saying if the value is
> different, return the delta and leave the value unchanged, but if the delta
> was zero between the expect and update, then the value wouldn't need
> changing anyway! You can do that with just a read.
>
> - DML
>
> On 02/10/2010 09:36 AM, Vladimir Blagojevic wrote:
>> Hi,
>>
>> Not sure if there are any concurrency enthusiasts here but I'll give it a
shot :)
>>
>> Would it be possible to make a lock-free, thread safe structure that would
essentially retain "boolean compareAndSet(long expect,long update)" semantics
but instead of returning boolean would return delta between actual underlying value and
update value? Delta of zero would therefore mean that value has been set.
>>
>> Vladimir
>>
>>
>>
>>
>> _______________________________________________
>> jboss-development mailing list
>> jboss-development(a)lists.jboss.org
>>
https://lists.jboss.org/mailman/listinfo/jboss-development
> _______________________________________________
> jboss-development mailing list
> jboss-development(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/jboss-development