How to avoid deadlocks? How to avoid deadlocks? multithreading multithreading

How to avoid deadlocks?


One way is to use a hierarchy of critical sections. If you ensure that a parent critical section is never entered within one of its children, deadlocks cannot happen. The difficulty is to enforce this hierarchy.


The Related list to the right on this page contains a few links that provides interesting information on the topic.

In addition to that list, there are many other SO questions discussing the topic, such as

...and many more


When I work in C++, the following works for me:

  1. all public methods (excluding ctor and dtor) of a threadsafe class lock

  2. private methods cannot call public methods

It's not a general deadlock avoidance method.