Multi-Threading: Threads.Atomic update value

What’s the difference between

  • a[] = true and
  • Threads.atomic_xchg!(a, true)

for a = Threads.Atomic{Bool}(false)?

I think the important difference is the return value. In the first case, the return value will be true; assignment just returns the value assigned.

In the second case, assuming a single threaded program, the value will be false since atomic_xchg! returns the old value of the reference. In a multi-threaded program where several threads execute atomic_xchg!, only one (the first to get there) will see false, the others will all see true since they see the result of the first atomic_xchg!.

1 Like