I am beginner in concurrent programming on C++. I read about std::atomic_flag and I don't understand where this atomic_flag can be useful. Maybe someone can explain me for which tasks atomic_flag can be useful
Asked
Active
Viewed 202 times
0
-
3Can you specify your question? Are you asking why `std::atomic_flag` exists when there is `std::atomic
`? If so, [duplicate here](https://stackoverflow.com/questions/39329311/difference-between-standards-atomic-bool-and-atomic-flag). Are you asking what atomics are? Then [duplicate here](https://stackoverflow.com/questions/31978324/what-exactly-is-stdatomic). – walnut Dec 05 '19 at 11:38 -
1It's useful when you need a _flag_ which must be _atomic_. If you don't know when you need a flag, or when something must be atomic, figure those out first. – Useless Dec 05 '19 at 12:19
1 Answers
6
cppreference.com has a usage example. It also contains the following explanatory note:
Unlike all specializations of
std::atomic
, it is guaranteed to be lock-free.
In other words: you’d use it the same way you would use std::atomic<bool>
but with the added guarantee of lock-freeness (though on most systems std::atomic<bool>
will also be lock-free, i.e. std::atomic<bool>::is_always_lock_free
will be true
).

Konrad Rudolph
- 530,221
- 131
- 937
- 1,214