Single-atom update ordering guarantees

2015-06-09 Thread Michael Gardner
This might be blindingly obvious to some, but I can't find any discussion about it. Let's say I have code like the following: (def a (atom 1)) ... (swap! a inc) (swap! a dec) Is there any possibility of another thread seeing a=0? If not, what provides this guarantee? -- You received this

Re: Single-atom update ordering guarantees

2015-06-09 Thread Andy Fingerhut
I am not sure why Atamert says No. If the (swap! a inc) and (swap! a dec) are executed in different threads, and they can occur in either order because there is no synchronization between those threads that prevents one of the two possible orders from occurring, then another thread *could* see a

Re: Single-atom update ordering guarantees

2015-06-09 Thread Atamert Ölçgen
On Tue, Jun 9, 2015 at 7:30 PM, Michael Gardner gardne...@gmail.com wrote: This might be blindingly obvious to some, but I can't find any discussion about it. Let's say I have code like the following: (def a (atom 1)) ... (swap! a inc) (swap! a dec) Is there any possibility of another

Re: Single-atom update ordering guarantees

2015-06-09 Thread Michael Gardner
I'm talking about a scenario where a single thread does inc, followed shortly by dec. Obviously from that thread's perspective, the value will never be zero, but what about as seen by other threads? My understanding of Java's memory model is that instructions within a single thread *can* get

Re: Single-atom update ordering guarantees

2015-06-09 Thread Andy Fingerhut
swap! is implemented using Java's AtomicReference class and its compareAndSet method. I haven't dug into the Java source code implementing that class, but you can read the Java documentation for all Atomic* Java classes here:

Re: Single-atom update ordering guarantees

2015-06-09 Thread Michael Gardner
Thanks. That does appear to provide the guarantee I was looking for. On Jun 9, 2015, at 12:14 PM, Andy Fingerhut andy.finger...@gmail.com wrote: swap! is implemented using Java's AtomicReference class and its compareAndSet method. I haven't dug into the Java source code implementing that

Re: Single-atom update ordering guarantees

2015-06-09 Thread Fluid Dynamics
On Tuesday, June 9, 2015 at 4:24:16 PM UTC-4, Michael Gardner wrote: Thanks. That does appear to provide the guarantee I was looking for. On Jun 9, 2015, at 12:14 PM, Andy Fingerhut andy.fi...@gmail.com javascript: wrote: swap! is implemented using Java's AtomicReference class and