Irmen de Jong wrote:
[EMAIL PROTECTED] 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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to