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 <[email protected]> wrote:

>
>
> On Tue, Jun 9, 2015 at 7:30 PM, Michael Gardner <[email protected]>
> 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 [email protected]
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> [email protected]
>> 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 [email protected].
>> 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 [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> 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 [email protected].
> 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 [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to