Created
July 16, 2019 01:59
-
-
Save eLtronicsVilla/60ceb1f61c9d60971a284a98c4c06f2a to your computer and use it in GitHub Desktop.
event creation for synchronization mechanism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, time | |
from threading import Event, Thread | |
event = Event() | |
def my_wait(event, nloops): | |
for i in range(nloops): | |
print("%s. Waiting for the flag to be set." % (i+1)) | |
event.wait() # Blocks until the flag becomes true. | |
print("Wait complete at:", time.ctime()) | |
event.clear() # Resets the flag | |
def my_set(event, nloops): | |
for i in range(nloops): | |
time.sleep(random.randrange(2, 5)) # Sleep some time. | |
event.set() | |
threads = [] | |
nloops = random.randrange(3, 6) | |
threads.append(Thread(target=my_wait, args=(event, nloops))) | |
threads[-1].start() | |
threads.append(Thread(target=my_set, args=(event, nloops))) | |
threads[-1].start() | |
for thread in threads: | |
thread.join() | |
print("All operation done.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment