小编Meh*_*and的帖子

并发运行两个线程,它们都操纵单个变量

这是我的代码:

import threading
x=0
class a(threading.thread)
    def run(self):
        global x
        for i in range(1000000):
             x+=1

class b(threading.thread)
    def run(self):
        global x
        for i in range(1000000):
             x-=1
def run():
    a().start()
    b().start()
    //after both thread done
    print x
run()
Run Code Online (Sandbox Code Playgroud)

我希望这显示我0(x = 0),但每次我运行它的结果是非常不同(小于零)
它有什么问题?

python concurrency multithreading race-condition

1
推荐指数
1
解决办法
95
查看次数