线程之间共享全局变量存在问题
2021-02-20 06:34
标签:glob name == read targe port join class lob 线程之间共享全局变量存在问题 标签:glob name == read targe port join class lob 原文地址:https://www.cnblogs.com/monsterhy123/p/12682729.html 1 import time
2 from threading import*
3 #定义全局变量num
4 num = 0
5 def test1():
6 global num
7 for i in range(100000):
8 num+=1
9 print(‘test1输出num:‘,num)
10
11 def test2():
12 global num
13 for i in range(100000):
14 num+=1
15 print(‘test2输出num:‘,num)
16
17 if __name__==‘__main__‘:
18 t1 = Thread(target=test1)
19 t2 = Thread(target=test2)
20 t1.start()
21 t2.start()
22 t1.join()
23 t2.join()
1 test1输出num: 100000
2 test2输出num: 146137
下一篇:线程池大小如何调?