Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threading
- i = 0
- THREAD_CNT = 10
- LOOP_CNT = 50000
- def foo():
- """This will increment the global variable i in a thread unsafe way"""
- global i
- for _ in range(LOOP_CNT):
- i += 1 # increments the global variable
- threads = [threading.Thread(target=foo) for _ in range(THREAD_CNT)]
- for t in threads:
- t.start() # starts each thread
- for t in threads:
- t.join() # waits for each thread to complete execution
- print("Expected: ".ljust(10), len(threads) * LOOP_CNT)
- print("Actual: ".ljust(10), i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement