Is there any reason to use Mutexes in C++ if I have access to atomic<bool>?

Viewed 94
   int data {} 
   atomic<bool> syncer {false};
   void foo()
   {
         syncer = true;  //---> I could have used a mutex here and writelock 
         data = 5; 
         syncer = false;
   }

   void foo2()
   {
       if (syncer)   // If I used a mutex I could have read lock here 
          return 0;
       return data;
   }

I always use atomics instead of mutexes. But today in a code review, I was told to use mutexes. Then I explained that atomics may not lock and this is an advantage over mutexes.

So, both my reviewer and I ended up with a question: What is the use of mutexes? Should we ban mutexes in our code guidelines and use atomics all the time? Is there a advantage of mutexes over atomics?

0 Answers
Related