thread lock object.
Peter Hansen
peter at engcorp.com
Sun Apr 24 12:02:44 EDT 2005
Irmen de Jong wrote:
> ajikoe at gmail.com wrote:
>> How can we make only one thread at a time be able to access and
>> increment i ?
>
> Use a synchronization primitive such as a lock
> (threading.Lock, threading.RLock)
>
> But for simply incrementing a number (i+=1) this is not needed
> because that operation cannot be interrupted by another thread,
> as far as I know.
Most assuredly it can:
>>> def f():
... global x
... x += 1
...
>>> dis.dis(f)
3 0 LOAD_GLOBAL 0 (x)
3 LOAD_CONST 1 (1)
6 INPLACE_ADD
...a context-change here can lead to incorrect results...
7 STORE_GLOBAL 0 (x)
10 LOAD_CONST 0 (None)
13 RETURN_VALUE
-Peter
More information about the Python-list
mailing list