Skip to content

Instantly share code, notes, and snippets.

@j178
Created January 11, 2021 13:16
Show Gist options
  • Save j178/48e587bfc10e66fc76c8223a4eec82a6 to your computer and use it in GitHub Desktop.
Save j178/48e587bfc10e66fc76c8223a4eec82a6 to your computer and use it in GitHub Desktop.
from threading import Event, Thread
event1 = Event()
event2 = Event()
event3 = Event()
i = 0
def f1():
global i
while True:
event1.wait()
print("t1", i)
i += 1
event2.set()
event1.clear()
def f2():
global i
while True:
event2.wait()
print("t2", i)
i += 1
event3.set()
event2.clear()
def f3():
global i
while True:
event3.wait()
print("t3", i)
i += 1
event1.set()
event3.clear()
if __name__ == '__main__':
t1 = Thread(target=f1)
t1.start()
t2 = Thread(target=f2)
t2.start()
t3 = Thread(target=f3)
t3.start()
event1.set()
t1.join()
t2.join()
t3.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment