标签:int 技术分享 == release 并且 pre mutex 死锁 时间
在线程间共享多个资源的时候,如果两个线程分别占有一部分资源并且同时等待对方的资源,就会造成死锁。
#coding=utf-8 import threading import time class MyThread1(threading.Thread): def run(self): if mutexA.acquire(): print(self.name+‘----do1---up----‘) time.sleep(1) if mutexB.acquire(): print(self.name+‘----do1---down----‘) mutexB.release() mutexA.release() class MyThread2(threading.Thread): def run(self): if mutexB.acquire(): print(self.name+‘----do2---up----‘) time.sleep(1) if mutexA.acquire(): print(self.name+‘----do2---down----‘) mutexA.release() mutexB.release() mutexA = threading.Lock() mutexB = threading.Lock() if __name__ == ‘__main__‘: t1 = MyThread1() t2 = MyThread2() t1.start() t2.start()
In [1]: import threading In [2]: mutexA = threading.Lock() In [3]: help(mutexA.acquire_lock)
、
标签:int 技术分享 == release 并且 pre mutex 死锁 时间
原文地址:http://www.cnblogs.com/venicid/p/7967027.html