1

Let's consider multicore CPU x86. Now, we have a some computation that is being executing by 4 threads. To make a computation it is necessary to take two locks (e.g. mutexes). So, it is clear to avoid deadlock we can take them atomically. It means: all mutexes are taken or not.

In C++11 it can be done with std::lock.

But, how it can be ensured that it is done atomically? I cannot imagine. I see how to do it on UP systems: just disable interrupts.

Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
  • I suppose that it is not atomic. But how it is solved. – Gilgamesz Mar 16 '17 at 21:19
  • You can see a solution in the boost source code, [reproduced here](http://stackoverflow.com/questions/9814008/multiple-mutex-locking-strategies-and-why-libraries-dont-use-address-comparison) – Raymond Chen Mar 16 '17 at 21:52
  • `std::lock` doesn't take them atomically with a guarantee. It can lock and roll back etc. It just guarantees no deadlocks with other calls to `std::lock`. – Yakk - Adam Nevraumont Mar 17 '17 at 01:01
  • Note that taking all mutexes atomically is not generally a requirement to avoid deadlocks. Rather, it is sufficient to just make sure any code that wants to hold more than one mutex at a time locks those mutexes in the same order as any other functions that locks those same mutexes do. – Jeremy Friesner Mar 17 '17 at 01:03

0 Answers0