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 reordered, and Java only guarantees that they will *appear* to 
have executed in order from the perspective of the executing thread. Other 
threads might see those instructions as executing out-of-order. What I'm 
wondering is whether atoms are subject to this as well.

> On Jun 9, 2015, at 10:16 AM, Andy Fingerhut <andy.finger...@gmail.com> wrote:
> 
> 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 value of 0, if and when the (swap! a dec) 
> occurs first.
> 
> If the (swap! a inc) and (swap! a dec) are executed sequentially in a single 
> thread, and no other thread is modifying a, then by normal sequential 
> execution the atom should change from 1 to 2, then from 2 back to 1.
> 
> Andy
> 
> On Tue, Jun 9, 2015 at 9:38 AM, Atamert Ölçgen <mu...@muhuk.com> wrote:
> 
> 
> 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 thread seeing a=0? If not, what provides 
> this guarantee?
> 
> No. Not if those two swap! calls are made from different threads.
>  
> 
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
> 
> 
> 
> -- 
> Kind Regards,
> Atamert Ölçgen
> 
> ◻◼◻
> ◻◻◼
> ◼◼◼
> 
> www.muhuk.com
> www.olcgen.com
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
> 
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to