is +=1 thread safe

Paul Rubin http
Fri May 2 16:14:37 EDT 2008


Hrvoje Niksic <hniksic at xemacs.org> writes:
> Since the += operator is not compiled into a single bytecode
> instruction, it needs the lock.

Aha, you are right.  What I was remembering is that xrange.next
is atomic in CPython, i.e. you can say something like

    counter = xrange(10000)
    
and then

   a = counter.next()

doesn't need a lock.  I am personally squeamish about relying on things
like this but apparently it is a standard idiom.  I will guess, but 
I haven't checked and I don't remember hearing, that itertools.count()
also works like that.



More information about the Python-list mailing list