Skip to content

Let's suffer one deadlock by not using logical layers

Francisco Ruiz edited this page Sep 29, 2015 · 8 revisions

Statement

We have bad news, you do not need more than one thread and one Mutex to generate a deadlock, as you can see you can generate it with only one thread and only one mutex which is being misused.

The good news is that this pattern of failure is easy to detect and it should be easy to solve.

You should study/compile/run the source code:

cd problems/02_auto_deadlock
make
./dirty

Let's start

  • Try to understand why/where/when threads start.
  • Try to understand what threads are running.
  • Try to understand why/where/when threads terminate.
  • What would happen without join the thread?
  • Try to think about how it is possible an only one thread locks itself.

Solution

The first way to solve the problem must be very straight forward, but you can read some cons about your possible solution in Recursive Lock (Mutex) vs Non-Recursive Lock (Mutex).

There is a second way to solve this problem but you should think about a solution based in logical layers.

Try to change the code in dirty to solve the problem. There is two main ways to solve the problem. Suggested Solutions.