import threading
# 全局解释器锁 GIL
# python 一个线程对应于c语言中的一个线程
import dis
# def add(a):
# a = a+1
# return a
# 反编译
# print(dis.dis(add))
# GIL根据执行的字节码行数、时间片,GIL在遇到IO操作主动释放
# GIL释放例子
total = 0
def add():
global total
for i in range(10000000):
total +=1 # 线程不安全
def desc():
global total
for i in range(10000000):
total -=1
thread1 = threading.Thread(target=add)
thread2 = threading.Thread(target=desc)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(total)
# 167499
# 1518478
多线程回顾-全局解释器锁GIL
猜你喜欢
转载自blog.csdn.net/qq_37463791/article/details/105027889
今日推荐
周排行