from threading import Lock
from multiprocessing import Process
from threading import Thread
#锁将并行操作变成串行操作
def work1():
global a,b
for i in range(1000000):
mutex.acquire()
a += 1
mutex2.acquire()
b += 1
mutex.release()
mutex2.release()
print('a=%d,b=%d'%(a,b))
def work2():
global a,b
for i in range(1000000):
mutex.acquire()
a+=1
mutex2.acquire()
b+=1
mutex2.release()
mutex.release()
print('a=%d,b=%d' % (a, b))
mutex=Lock()
mutex2=Lock()
if name == ‘main’:
t1=Thread(target=work1)
t2 = Thread(target=work2)
t1.start()
t2.start()