If I use atomic objects, I no longer need mutex (critical sections), right? or am I wrong?.
If I am wrong, could you give me a simple example (if possible in code) of when to use both, please?
¿Does Mutex generate atomicity like atomic objects?
If I am wrong, could you give me a simple example (if possible in code) of when to use both, please?
Ecample one, atomic:
std::atomic<bool> flag = false;
flag = true; // now flag can be checked from another thread
Example two. mutex:
SomeData *pointer1 = nullptr;
SomeOtherData *pointer2 = nullptr;
{
std::scoped_lock<std::mutex> lock( mux );
if( pointer1 && pointer2 ) {
doSomethingWithData( pointer1, pointer2 );
}
}
Examples are simplified of course, but should give you an idea.