Multithreading - Concurrent Programming (6) - Deadlock

What is deadlock:

Two or more threads block forever, waiting for each other's locks

It is a phenomenon in which the next group of concurrently competing threads of resources are permanently blocked due to waiting for each other.

example:

 explain:

 Thread a occupies object lock 1, thread b occupies object lock 2

Thread a needs to continue to occupy object lock 2 to execute, so thread a needs to wait for thread b to release object lock 2

Thread b needs to continue to occupy object lock 1 to execute, and thread a also needs to release object lock 1

Since these two threads do not release the locks they have occupied, these two threads will be in an infinite waiting state

In layman's terms:

In order to let the magic lamp enter the seal, the boy used the forbidden technique - deadlock

Magic Lamp: "Congratulations on finding me, I can fulfill your 2 wishes, please state your 2 wishes"

Boy: "Please grant my second wish"

Magic Lamp: "Okay, then what?"

Boy: "Please grant my first wish"

The magic lamp has entered a deadlock state, successfully sealed, and can no longer complete the task of fulfilling 2 wishes

There are 4 major factors that cause deadlocks:
Mutual exclusion: shared resources can only be occupied by one thread - mutex
occupied and waiting: threads currently occupying at least one resource and want to request other resources held by other threads will cause waiting - waiting for each other Released resources
cannot be preempted: the resource can only be released voluntarily by the thread holding it, and other threads cannot forcibly occupy the resource - cannot release the resource of the other party
Circular waiting: thread A waits for the resource occupied by thread B, thread B waits for the resource occupied by thread A, It's a circular wait - 2 threads wait for each other

Solving deadlocks:
You cannot directly remove deadlocks, which cannot guarantee thread safety. You
only need to destroy one of the four major factors that cause deadlocks to effectively solve and prevent deadlocks.

Destruction of mutual exclusion:
For example, if the code is to calculate the account amount, it can be operated through atomic calculation, so there will be no thread safety problems
or optimistic locking, as long as the mutual exclusion lock is not used, there will be no deadlock situation

Destroy possession and wait:
It can be solved by occupying multiple times; for example, the loop condition is used to determine whether there is any in the list, if not, pass in 2 resources, and sometimes return false to refuse to enter again

Destruction cannot be preempted:
use lock to set the timeout using its timeout mechanism; for
example: set 2s, if 2s has not been released, it will be automatically released

Destruction of circular waiting:
let it wait in an orderly manner; for
example: thread A locks 1 first, then locks 2, and marks it; then thread B also locks 1 first, and then locks 2 according to the mark

In this way, deadlock can be prevented, and damage will be completely eliminated;
if you want to effectively avoid it with a certain probability, you can understand the banker's algorithm.

Guess you like

Origin blog.csdn.net/weixin_59624686/article/details/124119558