3 ms·
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 unsyn
by nneonneo 5y ago
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.