is +=1 thread safe
John Nagle
nagle at animats.com
Thu May 1 11:40:05 EDT 2008
AlFire wrote:
> Hi,
> 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?
"counter += 1" is not an atomic operation, apparently. Sorry.
However, appending and removing from a list or dict are atomic operations,
because if they were not, memory allocation would break.
John Nagle
More information about the Python-list
mailing list