Is there a more efficient threading lock?

Barry Scott barry at barrys-emacs.org
Sun Feb 26 06:53:26 EST 2023


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.

Barry



More information about the Python-list mailing list