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 threading import Thread, Lock | |
class Adding: | |
def __init__(self): | |
self.added = False | |
self.value = 0 | |
def has_added(self): | |
return self.added | |
def add(self): | |
print "Adding...\n" | |
self.value +=1 | |
self.added = True | |
def get_value(self): | |
return self.value | |
def do_add(adding): | |
a_lock.acquire(1) | |
if not adding.has_added(): | |
adding.add() | |
a_lock.release() | |
a_lock = Lock() | |
adding = Adding() | |
for i in xrange(5): | |
Thread(target=do_add,args=(adding,)).start() | |
print "Value: %s" % adding.get_value() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment