From C++ Concurrency in Action:
difference between std::atomic and std::atomic_flag is that std::atomic may not be lock-free; the implementation may h
The standard does not garantee atomic objects are lock-free. On a platform that doesn't provide lock-free atomic operations for a type T
, std::atomic
objects may be implemented using a mutex, which wouldn't be lock-free. In that case, any containers using these objects in their implementation would not be lock-free either.
The standard provide an opportunity to check if an std::atomic
variable is lock-free: you can use var.is_lock_free()
or atomic_is_lock_free(&var)
. For basic types such as int, there is also macros provided (e.g. ATOMIC_INT_LOCK_FREE
) which specify if lock-free atomic access to that type is available.
std::atomic_flag
is an atomic boolean type. Almost always for boolean
type it's not needed to use mutex or another way for synchronization.