I'm attempting to comprehend the fundamentals of threading and concurrency. I need a straightforward situation where two strings consistently attempt to get to one shared asset.
The code:
import threading
class Thread(threading.Thread):
def __init__(self, t, *args):
threading.Thread.__init__(self, target=t, args=args)
self.start()
count = 0
lock = threading.Lock()
def incre():
global count
lock.acquire()
try:
count += 1
finally:
lock.release()
def bye():
while True:
incre()
def hello_there():
while True:
incre()
def main():
hello = Thread(hello_there)
goodbye = Thread(bye)
while True:
print count
if __name__ == '__main__':
main()
Thus, I have two strings, both attempting to increase the counter. I believed that if string 'A' called incre(), the lock would be set up, forestalling 'B' from getting to until 'A' has delivered
Can anyone tell me, how does it exactly was the lock object used?