is +=1 thread safe

Marc Christiansen usenet at solar-empire.de
Sun May 4 11:24:54 EDT 2008


Alexander Schmolck <a.schmolck at gmail.com> wrote:
> Gary Herron <gherron at islandtraining.com> writes:
> 
>> But... It's not!  
>>
>> A simple test shows that.   I've attached a tiny test program that
>> shows this extremely clearly.  Please run it and watch it fail.
> 
> In [7]: run ~/tmp/t.py
> final count: 2000000
>   should be: 2000000
> 
> (I took the liberty to correct your test to actually do what I said, namely
> use a numpy.array; just replace ``count = 0`` with ``import numpy; count =
> numpy.array(0)``).

I did the same change. Here are the results of four runs:

final count: 1999980
  should be: 2000000

final count: 1999996
  should be: 2000000

final count: 1999123
  should be: 2000000

final count: 1999978
  should be: 2000000

This is on a AMD64 X2 using Python 2.5.2 and numpy 1.0.4.

Marc



More information about the Python-list mailing list