import multiprocessing as mp
import threading as td
import time
def job(v,num,l):
l.acquire()
for _ in range(10):
time.sleep(0.1)
v.value += num
print(v.value)
l.release()
def multicore():
lock = mp.Lock()
#lock2 = mp.Lock() #两个锁不能达到锁的效果
v = mp.Value('i',0)
p1 = mp.Process(target=job,args=(v,1,lock))
p2 = mp.Process(target=job,args=(v,3,lock))
p1.start()
p2.start()
p1.join()
p2.join()
if __name__ == '__main__':
multicore()
#t1 = td.Thread(target=job,args=(1,2))
#t1.start()
#t1.join()
import threading as td
import time
def job(v,num,l):
l.acquire()
for _ in range(10):
time.sleep(0.1)
v.value += num
print(v.value)
l.release()
def multicore():
lock = mp.Lock()
#lock2 = mp.Lock() #两个锁不能达到锁的效果
v = mp.Value('i',0)
p1 = mp.Process(target=job,args=(v,1,lock))
p2 = mp.Process(target=job,args=(v,3,lock))
p1.start()
p2.start()
p1.join()
p2.join()
if __name__ == '__main__':
multicore()
#t1 = td.Thread(target=job,args=(1,2))
#t1.start()
#t1.join()