Python's global interpreter lock does not make counter += 1 atomic, because it compiles to separate load, add and store steps, so threads can still lose updates.
import threading
counter = 0
lock = threading.Lock()
def unsafe(n):
global counter
for _ in range(n):
counter += 1 # read-modify-write: updates can be lost
def safe(n):
global counter
for _ in range(n):
with lock: # only one thread in the critical section
counter += 1
threads = [threading.Thread(target=safe, args=(100_000,)) for _ in range(4)]
[t.start() for t in threads]
[t.join() for t in threads]
print(counter) # 400000 with the lock