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
ProfitServer has launched a promotion to celebrat...
This article briefly introduces the two forms of ...
In the article "Bitcoin Prequel", it is...
Kuroit is a foreign hosting company founded in 20...
With the advent of the big data era, all kinds of...
Riverbed Technology recently announced that the l...
The introduction of the number portability policy...
[[343025]] As the global energy crisis intensifie...
Remote control, as a new Internet operation mode,...
On December 16, 2020, F5 held an online press con...
For four consecutive years, the government work r...
[[397604]] This experiment uses the SIM7600CE 4G ...
[Hangzhou, China, May 25, 2020] Today, the Huawei...
Let's take a look at HostYun's informatio...
【51CTO.com Quick Translation】Wireless mesh networ...