Skip to content

Instantly share code, notes, and snippets.

@leonsas
Created May 19, 2017 19:47
Show Gist options
  • Save leonsas/db68f96df896c78f1d3810e068148d8c to your computer and use it in GitHub Desktop.
Save leonsas/db68f96df896c78f1d3810e068148d8c to your computer and use it in GitHub Desktop.
Grokking the GIL and atomic ops
# Tweaked from https://emptysqua.re/blog/grok-the-gil-fast-thread-safe-python/
import threading
for i in range(100):
n = 0
def foo():
global n
n += 1
threads = []
for i in range(1000):
t = threading.Thread(target=foo)
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
print(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment