Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Deadlock requires all four conditions, so , to prevent deadlock, prevent any one of the four conditions. This guideline recommends locking the mutexes in a predefined order to prevent circular wait.

...

The following code has behavior that depends on the runtime environment and the platform's scheduler. However, with proper timing, the main() function will deadlock when running thr1 and thr2, where thr1 tries to lock ba2's mutex, while thr2 tries to lock on ba1's mutex in the deposit() function, and the program will not progress.

...

The solution to the deadlock problem is to use a predefined order for the locks in the deposit() function. In the following compliant solution, each thread will lock based on the id of lock on the basis of the bank_account ID, defined  defined in the struct initialization. This solution prevents the circular wait problem.

...

Deadlock prevents multiple threads from progressing, thus halting the executing program. This is a potential  A denial-of-service attack is possible because the attacker can force deadlock situations. Deadlock is likely to occur in multithreaded programs that manage multiple shared resources.

...

MITRE CWE: CWE-764] Multiple Locks locks of Critical Resourcescritical resources

Bibliography

[Barney 2010] pthread_mutex tutorial
[Bryant 2003] Chapter 13, "Concurrent Programming"

...