600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 多线程共享全局变量

多线程共享全局变量

时间:2020-09-09 10:31:56

相关推荐

多线程共享全局变量

a=100

b=100

#多线程可以共享全局变量

from multiprocessing import Process

from threading import Thread

def work1():

global a,b

for i in range(3):

a+=1

b+=1

print(‘a=%d,b=%d’%(a,b))

def work2():

global a,b

for i in range(3):

a+=1

b+=1

print(‘a=%d,b=%d’ % (a, b))

ifname== ‘main’:

# t1=Thread(target=work1)

# t2 = Thread(target=work2)

# t1.start()

# t2.start()

p1=Process(target=work1)

p2 = Process(target=work2)

p1.start()

p2.start()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。