[erlang-questions] Lockfree now()

Erik Søe Sørensen <>
Thu Jul 12 20:33:55 CEST 2012


2012/7/12 Erik Søe Sørensen <>

> 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
> atomic.
>
>                 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.

... and the corresponding Erlang C operations appear to be
erts_smp_atomic_cmpxchg() and erts_smp_atomic_inctest().



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/eec40eae/attachment.html>


More information about the erlang-questions mailing list