[erlang-questions] Lockfree now()

Erik Søe Sørensen <>
Thu Jul 12 20:23:04 CEST 2012

Because of atomic operations.
I may not have been explicit enough there: latest_now is a
java.util.concurrent.AtomicLong, and the two updating accesses to it are
                long prev;
                while ((prev = latest_now.get()) < micros) {
                        if (latest_now.compareAndSet(prev,micros)) {
                                return micros;
                return latest_now.incrementAndGet();

This - and only because of this - means that no lock is necessary.

2012/7/12 Wojtek Narczyński <>

> On 07/12/2012 07:27 PM, Erik Søe Sørensen wrote:
>> Why not? That's certainly the intention of the last part; latest_now
>> should always be updated in a increasing manner, and the return value of
>> ensure_monotonically_**increasing_now() should always be equal to the
>> new value of latest_now.
>> Did I make a mistake?
> Well, how do you "ensure" it without taking a lock? Meanwhile the global
> value can be increased by another thread, to what you calculate locally.
> --Regards,
> Wojtek
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120712/d94ffb7f/attachment.html>

More information about the erlang-questions mailing list