Skip to content

Instantly share code, notes, and snippets.

@clayg
Created July 22, 2015 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clayg/d3630eeb3b6b680d631a to your computer and use it in GitHub Desktop.
Save clayg/d3630eeb3b6b680d631a to your computer and use it in GitHub Desktop.
import time
from threading import Thread, Lock
import itertools
c = itertools.count()
s = Lock()
a = 0
b = next(c)
z = 0
running = True
def incr():
global a
global b
global z
while running:
a += 1
b = next(c)
with s:
z += 1
threads = []
for i in range(10):
t = Thread(target=incr)
t.start()
threads.append(t)
try:
while True:
print a, b, z
time.sleep(1)
except KeyboardInterrupt:
running = False
for t in threads:
t.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment