News for hackers. Stuff that matters.
Re:Data races in Python, despite the Global Interpreter Lock by nneonneo on Monday February 21, 2022 @09:37PM (#30423452)
For what it’s worth, Python does have an atomic add instruction, but because integers are immutable, the load/store operations to the global variable are unsynchronized (the add produces a new object which must be stored to the global).

If you replace the Python integer with a 0D NumPy array, and use a real in-place operation, the race disappears:

    import numpy as np
    
    counter = np.array(0, dtype=np.uint32)
    one = np.uint32(1)
    
    def increase():
        global counter
        for i in range(0, 100000):
            counter += one
Of course, the lesson still holds: accessing shared state from multiple threads still requires care; you don’t get to ignore these things just because Python has a GIL.

There are no comments on this comment yet.