Skip to content

Instantly share code, notes, and snippets.

@amraks
Created October 13, 2017 23:45
Show Gist options
  • Save amraks/a775f96f2fc2a65041d04f28479e457c to your computer and use it in GitHub Desktop.
Save amraks/a775f96f2fc2a65041d04f28479e457c to your computer and use it in GitHub Desktop.
import threading
c = threading.Condition()
arr = [i-1 for i in range(1, 101)]
def odd():
for i in range(1, 100, 2):
with c:
print 'odd: ', arr[i]
c.notify()
c.wait()
def even():
for i in range(0, 100, 2):
with c:
print 'even: ', arr[i]
c.notify()
c.wait()
with c:
c.notify()
t1 = threading.Thread(target=even)
t2 = threading.Thread(target=odd)
t1.start()
t2.start()
t1.join()
t2.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment