Python multi-thread programming (5): deadlock formation and python multi-thread programming

Source: Internet
Author: User

Python multi-thread programming (5): deadlock formation and python multi-thread programming

Python in the previous article: using the threading module to implement multi-threaded programming 4 [using the Lock mutex Lock] We have begun to talk about how to use the mutex Lock to protect our public resources. Now, consider the following situation-

If there are multiple public resources that are shared among multiple threads, if the two threads occupy a part of the resources and wait for the resources of the other thread at the same time, what problems will this cause?

Deadlock

Deadlock: a deadlock occurs when two or more processes compete for resources during execution. If there is no external force, they will not be able to proceed. It is said that the system is in a deadlock state or the system has a deadlock. These processes that are always waiting for each other are called deadlock processes. Because resource usage is mutually exclusive, after a process applies for resources, the relevant process will never be allocated with necessary resources and cannot continue to run without external assistance, this produces a special deadlock.
Copy codeThe Code is as follows:
'''
Created on 2012-9-8
 
@ Author: walfred
@ Module: thread. TreadTest5
'''
Import threading
 
Countid = 0
CounterB = 0
 
MutexA = threading. Lock ()
MutexB = threading. Lock ()
 
Class MyThread (threading. Thread ):
Def _ init _ (self ):
Threading. Thread. _ init _ (self)
 
Def run (self ):
Self. fun1 ()
Self. fun2 ()
 
Def fun1 (self ):
Global mutexA, mutexB
If mutexA. acquire ():
Print "I am % s, get res: % s" % (self. name, "ResA ")
 
If mutexB. acquire ():
Print "I am % s, get res: % s" % (self. name, "ResB ")
MutexB. release ()
 
MutexA. release ()
 
Def fun2 (self ):
Global mutexA, mutexB
If mutexB. acquire ():
Print "I am % s, get res: % s" % (self. name, "ResB ")
 
If mutexA. acquire ():
Print "I am % s, get res: % s" % (self. name, "ResA ")
MutexA. release ()
 
MutexB. release ()
 
If _ name _ = "_ main __":
For I in range (0,100 ):
My_thread = MyThread ()
My_thread.start ()

The Code shows that two function functions of a thread obtain another competing resource after obtaining one competing resource. Let's see the running result:
Copy codeThe Code is as follows:
I am Thread-1, get res: ResA
I am Thread-1, get res: ResB
I am Thread-2, get res: ResAI am Thread-1, get res: ResB

We can see that the program has been suspended. This phenomenon is called a "deadlock".

Avoid deadlocks

The main method to avoid deadlocks is to properly and orderly allocate resources and avoid deadlock. The most representative algorithm is the Banker algorithm proposed by Dijkstra E.W in 1968.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.