atomic increment

Paul Rubin http
Wed Aug 27 01:39:56 EDT 2008


Frank Millman <frank at chagford.com> writes:
> Just out of interest, would the following, without a lock, be safe?
> old, atomic_int = atomic_int, atomic_int+1
        
No I don't think so.  But I'm told that in CPython, you can say
   
  counter = iter(xrange(10000000))  # some number that exceeds what you'll use
  ...

  atomic_int = counter.next()

and the GIL keeps it safe.  When in doubt, use a lock or communicate
with other threads through Queues.



More information about the Python-list mailing list