Re:Data races in Python, despite the Global Interpreter Lock
by nneonneo on Monday February 21, 2022 @06:05PM
(#30421928)
A bit more detail: there is an atomic addition bytecode, called INPLACE_ADD, which is why something like “a_list += [1]” is actually atomic. The problem is that integers are immutable, so “counter += 1” involves a (non-atomic) load and store of counter, and that’s where the race is.
Hypothetically, if Python had a built-in mutable integer type, the increment could be made atomic.
Re:Data races in Python, despite the Global Interpreter Lock
by jasonhansel on Monday February 21, 2022 @05:57PM
(#30421832)
In general, "+=" probably needs to be non-atomic to support "__add__" overloading; Python wouldn't be able to call arbitrary "__add__" methods in a way that could guarantee atomicity.
Here is an example what thread safe increment looks like in Python:
https://julien.danjou.info/atomic-lock-free-counters-in-pyth...
You need to lock it explicitly.
Note that `INC` instructor for x86 architecture needs explicit hints/locks as well, so this should suprise anyone:
https://stackoverflow.com/q/10109679