-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrace_condition.py
58 lines (44 loc) · 1.48 KB
/
race_condition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import threading
import time
x = 0
def increment_global():
global x
x += 1
def task_of_thread(lock=None):
# define a range of 50.000, since we have two threads
# the end result should be 100.000
for _ in range(50000):
if lock is not None:
lock.acquire()
increment_global()
if lock is not None:
lock.release()
def main(fixed=False):
global x
x = 0
# create two threads to demonstrate race condition
t1 = threading.Thread(target=task_of_thread)
t2 = threading.Thread(target=task_of_thread)
if fixed:
# introduce a lock
lock = threading.Lock()
t1 = threading.Thread(target=task_of_thread, args=(lock,))
t2 = threading.Thread(target=task_of_thread, args=(lock,))
t1.start()
t2.start()
"""
https://stackoverflow.com/questions/15085348/what-is-the-use-of-join-in-python-threading
join([timeout]) Wait until the thread terminates. This blocks the calling thread until the thread whose join()
method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.
"""
t1.join()
t2.join()
if __name__ == "__main__":
# indicate if you want with or without race condition fix
fixed = False
start = time.time()
for i in range(3):
main(fixed)
print("x = {1} after Iteration {0}".format(i, x))
end = time.time()
print("Total time it took: {0}s".format(end-start))