Search code examples
operating-systemlockingmutexatomic

What does it mean to perform mutex locks functions atomically?


It is stated that calls to either acquire() or release() must be performed atomically. What do they mean by that?

acquire() {
   while (!available)
      ; /* busy wait */
   available = false;;
}
   do {
      (acquire lock)
         critical section
      (release lock)
         remainder section
   } while (true);

release() {
   available = true;
}

Solution

  • An atomic operation means that either it will be fully completed or not completed at all. The operation cannot be stopped/killed/ended in the MIDDLE.

    Atomic operations are used in case of multithreaded programming mostly. These operations are used to keep the sanity of critical section/variable sane, as many threads race for their execution.

    A good place to read about atomic operations and concurrency in C++ is "Concurrency in Action" by Anthony Williams