0

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 4
    Not all objects can be atomic but if you are sharing only atomic types between threads then you shouldn't need a mutex. One still might be needed depending on what all else you are doing though so this is my long way of saying: It depends – NathanOliver Feb 09 '23 at 18:18
  • 2
    If you only use a **single** atomic object, you are fine. As soon as you use several objects at once, you need a lock to stop others from interfering inbetween operations. – BoP Feb 09 '23 at 18:19
  • The edits are interesting. – sweenish Feb 09 '23 at 18:27
  • Hi @NathanOliver thanks + 1, ¿Does Mutex generate atomicity like atomic objects? –  Feb 09 '23 at 18:29
  • Hi @sweenish :) –  Feb 09 '23 at 18:30
  • 3
    @Coder23 Kind of. A mutex allows you to synchronize a block of code, while an atomic object just synchronizes that one object. – NathanOliver Feb 09 '23 at 19:11
  • @NathanOliver In other words, mutex protects code, and an atomic object protects **data** (**the variable itself**), ¿right? +1 –  Feb 09 '23 at 19:14
  • 2
    @Coder23 The both protect data, but mutex lets you make sure that multiple statements are ran together without another thread getting in the way. With atomic objects there is no synchronization between separate objects so threads could interleave those statements. – NathanOliver Feb 09 '23 at 19:25
  • great @NathanOliver +1 now I understand. Last question to verify that I understood, if I have several atomic objects in a thread, I will need a mutex, right? **since the operation would stop being atomic**. –  Feb 09 '23 at 19:29
  • 2
    If you want the total order of the operations of those individual operations to be maintained then you will need a mutex otherwise operations could be interleaved. – NathanOliver Feb 09 '23 at 19:31
  • great, thank you very much, you are a master in this +1 –  Feb 09 '23 at 19:32

1 Answers1

0

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.

Slava
  • 43,454
  • 1
  • 47
  • 90
  • hi @Slava +1 - if I have several atomic objects in a thread, I will need a mutex, right?Since the operation **would no longer be atomic** –  Feb 09 '23 at 19:21
  • @Coder23: If you need to make an atomic transaction involving a group of multiple objects, yes, you should use a mutex. (Or if they're small enough to pack into a 16-byte or smaller struct, a `compare_exchange_weak` retry loop will be lock-free on some systems, like modern x86-64 or AArch64.) – Peter Cordes Feb 09 '23 at 20:47
  • hi @Peter Cordes +1, thanks. With a mutex we also achieve **atomicity**, right? –  Feb 09 '23 at 21:10
  • 1
    @Coder23: If every other thing that accesses this data respects the mutex, then yes, of course. – Peter Cordes Feb 09 '23 at 21:13
  • 2
    @Coder23 "if I have several atomic objects in a thread, I will need a mutex, right?" no quite, you do not need atomic objects in this case you just need a mutex. – Slava Feb 09 '23 at 21:45