不要指望x += 1
是线程安全的。这里是an example它不工作(见约西亚卡尔森的评论):
import threading
x = 0
def foo():
global x
for i in xrange(1000000):
x += 1
threads = [threading.Thread(target=foo), threading.Thread(target=foo)]
for t in threads:
t.daemon = True
t.start()
for t in threads:
t.join()
print(x)
如果你拆开foo
:
In [80]: import dis
In [81]: dis.dis(foo)
4 0 SETUP_LOOP 30 (to 33)
3 LOAD_GLOBAL 0 (xrange)
6 LOAD_CONST 1 (1000000)
9 CALL_FUNCTION 1
12 GET_ITER
>> 13 FOR_ITER 16 (to 32)
16 STORE_FAST 0 (i)
5 19 LOAD_GLOBAL 1 (x)
22 LOAD_CONST 2 (1)
25 INPLACE_ADD
26 STORE_GLOBAL 1 (x)
29 JUMP_ABSOLUTE 13
>> 32 POP_BLOCK
>> 33 LOAD_CONST 0 (None)
36 RETURN_VALUE
你看,有一个LOAD_GLOBAL
检索的x
值,有一个INPLACE_ADD
,然后是一个STORE_GLOBAL
。
如果两个线程LOAD_GLOBAL
相继,那么它们可能都会加载相同的值x
。然后他们都增加到相同的数字,并存储相同的数字。所以一个线程的工作会覆盖另一个线程的工作。这不是线程安全的。
正如你所看到的,x
的最终值是2000000,如果程序是线程安全的,而是你几乎总是得到一个数小于2000000
如果添加了锁,你得到 “预期” 的答案:
import threading
lock = threading.Lock()
x = 0
def foo():
global x
for i in xrange(1000000):
with lock:
x += 1
threads = [threading.Thread(target=foo), threading.Thread(target=foo)]
for t in threads:
t.daemon = True
t.start()
for t in threads:
t.join()
print(x)
产生
2000000
我想,为什么你发布的代码不呈现问题的原因:
for i in range(1000):
t = threading.Thread(target = worker)
threads.append(t)
t.start()
是因为你的worker
氏完全这么混账的迅速相比,它需要产生一个新的线程,在实践中存在的时间线程之间没有竞争。在上面的Josiah Carlson的例子中,每个线程在foo
中花费了大量的时间,这增加了线程碰撞的机会。
由于GIL我猜 – wim 2013-02-25 01:53:44
特别是,CPython的确如此。 – Amber 2013-02-25 01:54:39
@Mike什么操作是原子使用GIL时? – remykits 2013-02-25 01:59:12