Alex Rivera | Logout

Threads and simple Dead lock cure

Asked 2009-12-12T07:04:36.270
10

When dealing with threads (specifically in C++) using mutex locks and semaphores is there a simple rule of thumb to avoid Dead Locks and have nice clean Synchronization?

Edit
Report

1 Answer

8
  1. If at all possible, design your code so that you never have to lock more then a single mutex/semaphore at a time.
  2. If that's not possible, make sure to always lock multiple mutex/semaphores in the same order. So if one part of the code locks mutex A and then takes semaphore B, make sure that no other part of the code takes semaphore B and then locks mutex A.
answered 2009-12-12T07:15:12.187

Your Answer