Threading Deadlock and reentrant locks in python

Source: Internet
Author: User


One, Dead lock

In simple terms, a deadlock is a resource that is invoked multiple times and a deadlock occurs when multiple callers fail to release the resource, combining examples to illustrate the two common deadlocks.

1. Iterative deadlock

This scenario is a thread "iteration" that requests the same resource, which can result in a deadlock:

Import threading
Import time
Class Mythread (threading. Thread):
def run (self):
Global num
Time.sleep (1)
If Mutex.acquire (1):
num = num+1
msg = self.name+ ' Set num to ' +str (num)
Print msg
Mutex.acquire ()
Mutex.release ()
Mutex.release ()
num = 0
Mutex = Threading. Lock ()
def test ():
For I in range (5):
t = Mythread ()
T.start ()
if __name__ = = ' __main__ ':
Test ()
In the example above, the first time a resource is requested in the if judgment of the run function, the request is not released, again acquire, eventually unable to release, resulting in deadlock. In this example, you can do this by commenting out the two lines below the print, which can be done in addition to a reentrant lock, which is mentioned later.

2, Mutual call deadlock

The deadlock in the example above is caused by multiple invocations within the same Def function, and in the other case where the same resources are invoked in two functions, waiting for each other to end. If two threads share a portion of the resources and wait for each other's resources, a deadlock is created.

Import threading
Import time
Class Mythread (threading. Thread):
def do1 (self):
Global ResA, Resb
If Mutexa.acquire ():
msg = self.name+ ' Got ResA '
Print msg
If Mutexb.acquire (1):
msg = self.name+ ' Got Resb '
Print msg
Mutexb.release ()
Mutexa.release ()
def do2 (self):
Global ResA, Resb
If Mutexb.acquire ():
msg = self.name+ ' Got Resb '
Print msg
If Mutexa.acquire (1):
msg = self.name+ ' Got ResA '
Print msg
Mutexa.release ()
Mutexb.release ()
def run (self):
Self.do1 ()
Self.do2 ()
ResA = 0
RESB = 0
Mutexa = Threading. Lock ()
MUTEXB = Threading. Lock ()
def test ():
For I in range (5):
t = Mythread ()
T.start ()
if __name__ = = ' __main__ ':
Test ()
The example of this deadlock is a little more complicated. Specific can be adjusted.

Second, can be reset lock

To support multiple requests for the same resource on the same thread, Python provides a "reentrant lock": Threading. Rlock. Rlock maintains a lock and a counter variable, counter records the number of acquire, so that resources can be require multiple times. Until all the acquire of a thread has been release, other threads will be able to obtain resources. Here, for example 1, if you use Rlock instead of lock, a deadlock will not occur:

Import threading
Import time
Class Mythread (threading. Thread):
def run (self):
Global num
Time.sleep (1)
If Mutex.acquire (1):
num = num+1
msg = self.name+ ' Set num to ' +str (num)
Print msg
Mutex.acquire ()
Mutex.release ()
Mutex.release ()
num = 0
Mutex = Threading. Rlock ()
def test ():
For I in range (5):
t = Mythread ()
T.start ()
if __name__ = = ' __main__ ':
Test ()

The difference between the 1 and the example is threading. Lock () replaced by threading. Rlock ().

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.