Last active
October 7, 2017 02:38
-
-
Save jcmiller11/8004682606be21d167bf5fad65106c44 to your computer and use it in GitHub Desktop.
Random Walk Using Race Conditions
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
from __future__ import print_function | |
from threading import Thread | |
from time import sleep | |
WALKNUM = 0 | |
STEPNUM = 0 | |
KILL = False | |
def increment(value): | |
global WALKNUM | |
global STEPNUM | |
while not KILL: | |
WALKNUM += value | |
STEPNUM += 1 | |
T1 = Thread(target=increment, args=(1, )) | |
T2 = Thread(target=increment, args=(-1, )) | |
T1.start() | |
T2.start() | |
while not KILL: | |
try: | |
sleep(1) | |
print(str(WALKNUM)+" After "+str(STEPNUM)+" Steps") | |
except KeyboardInterrupt: | |
KILL = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment