-
-
Save toandaominh1997/b87e7fb58a5602f37cefed16a03396b1 to your computer and use it in GitHub Desktop.
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
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