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
[Ruijie Networks Press Release] On April 10, Ruij...
[[416919]] Image source: https://pixabay.com/imag...
At the end of the TV series "Bright Sword&qu...
Digital transformation is in full swing, and tech...
Using bits to drive watts is one of the dreams of...
Everyone knows that when flying, you should turn ...
According to Yonhap News Agency, Hanwha Systems, ...
While other vendors are offering KVM, EtherNetser...
[51CTO.com original article] The sudden outbreak ...
ITLDC's Black Friday promotion this year last...
Recently, the concept of "Internet +" h...
RackNerd has launched some promotions in Los Ange...
Edge devices are more than just hardware, and wit...
who I am Hello everyone, my name is OpenStack, a ...
[[256713]] Let's look at a picture first. Fro...