线程的全局变量

多线程-共享全局变量

from threading import Thread
import time

g_num = 100

def work1():
    global g_num
    for i in range(3):
        g_num += 1

    print("----in work1, g_num is %d---"%g_num)


def work2():
    global g_num
    print("----in work2, g_num is %d---"%g_num)


print("---线程创建之前g_num is %d---"%g_num)

t1 = Thread(target=work1)
t1.start()

#延时一会,保证t1线程中的事情做完
time.sleep(1)

t2 = Thread(target=work2)
t2.start()

输出:

E:\chen\app\Python\python.exe E:/chen/课件/线程.py
---线程创建之前g_num is 100---
----in work1, g_num is 103---
----in work2, g_num is 103---

用列表方式传递线程

from threading import Thread
import time

def work1(name):
    name.append('张三')
    print('添加后为:',name)

def work2(name):
    time.sleep(1)
    print('是否和work1相同:',name)

num =['小明,老李']

t1=Thread(target=work1,args=(num,))
t1.start()
t2=Thread(target=work2,args=(num,))
t2.start()

总结:
在一个进程内的所有线程共享全局变量,能够在不适用其他方式的前提下完成多线程之间的数据共享(这点要比多进程要好)
缺点就是,线程是对全局变量随意遂改可能造成多线程之间对全局变量的混乱(即线程非安全)重点内容

你可能感兴趣的:(线程的全局变量)