Skip to content

Instantly share code, notes, and snippets.

@denisfitz57
Forked from jcmiller11/walk.py
Created April 1, 2016 16:55
Show Gist options
  • Save denisfitz57/5116e3be08eaea7921af53c77dc3a090 to your computer and use it in GitHub Desktop.
Save denisfitz57/5116e3be08eaea7921af53c77dc3a090 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