Skip to content

Instantly share code, notes, and snippets.

@eLtronicsVilla
Created July 16, 2019 01:39
Show Gist options
  • Save eLtronicsVilla/d1063005d6b25df294028b37149f933a to your computer and use it in GitHub Desktop.
Save eLtronicsVilla/d1063005d6b25df294028b37149f933a to your computer and use it in GitHub Desktop.
Locking mechanism for synchronization of a Thread
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