Is there a more efficient threading lock?

Jon Ribbens jon+usenet at unequivocal.eu
Sun Feb 26 11:11:11 EST 2023


On 2023-02-26, Barry Scott <barry at barrys-emacs.org> wrote:
> On 25/02/2023 23:45, Jon Ribbens via Python-list wrote:
>> I think it is the case that x += 1 is atomic but foo.x += 1 is not.
>
> No that is not true, and has never been true.
>
>:>>> def x(a):
>:...    a += 1
>:...
>:>>>
>:>>> dis.dis(x)
>   1           0 RESUME                   0
>
>   2           2 LOAD_FAST                0 (a)
>               4 LOAD_CONST               1 (1)
>               6 BINARY_OP               13 (+=)
>              10 STORE_FAST               0 (a)
>              12 LOAD_CONST               0 (None)
>              14 RETURN_VALUE
>:>>>
>
> As you can see there are 4 byte code ops executed.
>
> Python's eval loop can switch to another thread between any of them.
>
> Its is not true that the GIL provides atomic operations in python.

That's oversimplifying to the point of falsehood (just as the opposite
would be too). And: see my other reply in this thread just now - if the
GIL isn't making "x += 1" atomic, something else is.


More information about the Python-list mailing list