Created
July 16, 2019 01:39
-
-
Save eLtronicsVilla/d1063005d6b25df294028b37149f933a to your computer and use it in GitHub Desktop.
Locking mechanism for synchronization of a Thread
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 Lock, Thread | |
lock = Lock() | |
value = 1 | |
def add_one(): | |
global value | |
lock.acquire() | |
value += 1 | |
lock.release() | |
def add_two(): | |
global value | |
lock.acquire() | |
value += 2 | |
lock.release() | |
threads = [] | |
for func in [add_one, add_two]: | |
threads.append(Thread(target=func)) | |
threads[-1].start() | |
for thread in threads: | |
""" | |
Waits for threads to complete before moving on with the main | |
""" | |
thread.join() | |
print(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment