#gil global interpreter lock (cpython)
#python中一个线程对应于c语言中的一个线程
#gil使得同一个时刻只有一个线程在一个cpu上执行字节码, 无法将多个线程映射到多个cpu上执行
#gil会根据执行的字节码行数以及时间片释放gil,gil在遇到io的操作时候主动释放
# import dis
# def add(a):
# a = a+1
# return a
#
# print(dis.dis(add))
total = 0
def add():
#1. dosomething1
#2. io操作
# 1. dosomething3
global total
for i in range(1000000):
total += 1
def desc():
global total
for i in range(1000000):
total -= 1
import threading
thread1 = threading.Thread(target=add)
thread2 = threading.Thread(target=desc)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(total) #可以看到这个例子,每次结果都是不一样的,而我们的预期结果是要 total为0
from threading import Lock
total = 0
lock = Lock()
def add():
global lock
global total
for i in range(1000000):
lock.acquire() #在使用变量时,锁住,不让线程切到其他地方去
total += 1
lock.release() #执行完之后就释放掉,
def desc():
global total
global lock
for i in range(1000000):
lock.acquire()
total -= 1
lock.release()
import threading
thread1 = threading.Thread(target=add)
thread2 = threading.Thread(target=desc)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(total) #加了锁之后,就是0了,这个时候就符合我们的预期了
#有时,需要两次acquire(),比方说你调了其他线程的函数,里面也包含了acquire(),就会引起死锁
# 解决第一种死锁,连续acquire两次的问题,需要用到 Rlock 可重入的锁
#在同一个线程里面,可以连续调用多次acquire, 一定要注意acquire的次数要和release的次数相等
from threading import RLock
lock = RLock()
def add():
global lock
global total
for i in range(1000000):
lock.acquire()
lock.acquire()
total += 1
lock.release()
lock.release()