is +=1 thread safe

Carl Banks pavlovevidence at gmail.com
Sun May 4 02:29:17 EDT 2008


On May 3, 7:44 pm, Gary Herron <gher... at islandtraining.com> wrote:
> Alexander Schmolck wrote:
> > AlFire <spamgrinder.tryla... at ggmail.com> writes:
>
> >>> The threading module already has a function to return the number of Thread
> >>> objects currently alive.
>
> >> I have threads within threads - so it does not suit me :-(.
>
> > How about using a scalar numpy array? They are mutable, so I assume that x +=
> > 1 should be atomic.
>
> No NO NO! The only way to increment a variable in memory is through a
> three step process:

Yes, this will be atomic.  It's a pretty good idea, in fact.  The
underlying increment operation is protected by the GIL; it could be
three, forty, or a hundred steps and it'd be atomic at the Python
level.


Carl Banks



More information about the Python-list mailing list