A Counter Race Despite the GIL
import threading
counter = 0
def increment():
global counter
for _ in range(1_000_000):
counter += 1
threads = [threading.Thread(target=increment) for _ in range(4)]
for t in threads:
t.start()
for t in threads:
t.join()
print(counter)
Four threads each increment the same global counter one million times. In CPython, is print(counter) guaranteed to print 4000000?
Sign in to answer questions and track your progress
Sign In