Created
September 8, 2013 05:54
-
-
Save akshar-raaj/6482219 to your computer and use it in GitHub Desktop.
A simple program demonstrating race condition
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 threading | |
#define a global variable | |
some_var = 0 | |
class IncrementThread(threading.Thread): | |
def run(self): | |
#we want to read a global variable | |
#and then increment it | |
global some_var | |
b = some_var | |
print "some_var in %s is %d" % (self.name, b) | |
some_var = b + 1 | |
print "some_var in %s after increment is %d" % (self.name, some_var) | |
if __name__ == "__main__": | |
for i in range(10): | |
t = IncrementThread() | |
t.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment