Skip to content

Instantly share code, notes, and snippets.

@toandaominh1997
Created September 20, 2020 02:29
Show Gist options
  • Save toandaominh1997/b87e7fb58a5602f37cefed16a03396b1 to your computer and use it in GitHub Desktop.
Save toandaominh1997/b87e7fb58a5602f37cefed16a03396b1 to your computer and use it in GitHub Desktop.
import threading
x = 0
def increament():
global x
x +=1
def thread_task(lock):
for _ in range(100000):
lock.acquire()
increament()
lock.release()
def main_task():
global x
x = 0
lock = threading.Lock()
t1 = threading.Thread(target=thread_task, args=(lock, ))
t2 = threading.Thread(target=thread_task, args=(lock, ))
t3 = threading.Thread(target=thread_task, args=(lock, ))
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
if __name__=='__main__':
for i in range(10):
main_task()
print('Iterator {}: x = {}'.format(i, x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment