《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

42 lines
1.1 KiB

import threading
counterA = 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()