Skip to content

Instantly share code, notes, and snippets.

@jcmiller11
Last active October 7, 2017 02:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jcmiller11/8004682606be21d167bf5fad65106c44 to your computer and use it in GitHub Desktop.
Save jcmiller11/8004682606be21d167bf5fad65106c44 to your computer and use it in GitHub Desktop.
Random Walk Using Race Conditions
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