std::atomic
(C++)
Wow I am so uneducated about the Multithreading world.
There are conditions when an object is at the same time accessed by different threads, resulting in an undefined behavior. This situation is called Data race.
C++ has an Atomic Type to solve this problem of Data Race.
[question] Why
std::atomic
when you havestd::mutex
??
std::atomic
provides a more lightweight and efficient mechanism for concurrency when dealing with simple data, whereasstd::mutex
offers broader control over critical sections but comes with more overhead.
Resources