AtomicBool or Mutex<bool>?


#1

As from the title, is it better the one or the other?
In which situation should be preferred which?


#2

You should use AtomicBool in every situation where its methods do what you want. AtomicXyz types compile down to extremely fast and memory-efficient hardware implementations.

Mutexes are useful when you want to atomically carry out some operation which is NOT atomic in hardware, such as filling up a 2 MB array for example. In this situation, you really don’t want another thread to be able to observe an inconsistent program state while the write is ongoing. So you prevent it from accessing the inconsistent data with a mutex.