is +=1 thread safe

AlFire spamgrinder.trylater at ggmail.com
Thu May 1 10:56:45 EDT 2008


Hi,

I have a piece of software which uses threads in very massive way - like 
hundreds of  them generated every second.

there is also a piece of code which maintains the number of outstanding 
threads, simply

counter+=1 is executed when before starting the thread and counter-=1 
after it finishes.

all is very simple and by the end of the program life I expect the 
counter to zero out.

however I am getting values -1, -2, 1 ,2 ,3 and quite often 0 as expected.

I guarded those statement with Lock.{acquire,release} and now it always 
returns 0.


But I still can not believe that +=1 is not a thread safe operation.


Any clue?

-- 

Andy



More information about the Python-list mailing list