Fans’ questions must be arranged. How can two threads and two mutexes deadlock? The program flow chart is as follows: Program flow chart As shown in the figure above:
[Note] To ensure that the main thread and child thread can obtain the locks mutex1 and mutex2 respectively, each thread must sleep for 1 second after obtaining the lock. Otherwise, after the child thread is created, the main thread still has a certain time slice, and the main thread will apply for the lock mutex2, and a deadlock cannot be formed. Deadlock The source code is as follows
Compile and run From the execution results, we can judge that the main thread and the child thread obtained the mutex locks mutex1 and mutex2 respectively. After sleeping for 1 second, they both wanted to apply for mutex2 and mutex1 respectively, but neither side wanted to release the locks in their hands. The locks have formed a deadlock, and the program has been in a dormant state. Check the threads of the process Check the process ID, which is 4204 Check the thread IDs created by the process: 4204, 4205. This article is reprinted from the WeChat public account "Yikou Linux", which can be followed through the following QR code. To reprint this article, please contact Yikou Linux public account. |
<<: Foreign media: Global investment and deployment in 5G will accelerate in 2020
The optical module power is abnormal! What should...
Data centers are the infrastructure for modern bu...
The development stage of medical informationizati...
80VPS is an early established Chinese hosting com...
[[188583]] "E-government is currently in a p...
RackNerd released a promotional package for June,...
POE power supply technology has become the darlin...
New! LOCVPS sent a message about a new VPS in Syd...
DediPath has released a Chinese New Year promotio...
According to RT Insights, the Manufacturing Insti...
On November 7, Li Zhengmao, general manager of Ch...
F5 recently announced the launch of F5 NGINX for ...
[[395494]] 1. Everything search tool Everything i...
As a network administrator or network engineer, i...