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 have std::mutex??

std::atomic provides a more lightweight and efficient mechanism for concurrency when dealing with simple data, whereas std::mutex offers broader control over critical sections but comes with more overhead.

Resources