-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrace_condition.py
74 lines (60 loc) · 1.1 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from threading import Thread, Lock
from time import sleep
counter = 0
# Our function
def increment(num, lock):
global counter
#lock.acquire()
local_counter = counter
local_counter += num
sleep(0.1) # creates the
counter = local_counter
print (f"counter={counter}")
#lock.release()
# create lock object
lock = Lock()
# Threads
t1 = Thread(target=increment,args=(10,lock))
t2 = Thread(target=increment,args=(20,lock))
# Start Threads
t1.start()
t2.start()
# Wait for threads to complete
t1.join()
t2.join()
print(f"The final count is: {counter}")
#
# from threading import Thread
# from time import sleep
#
#
# counter = 0
#
# def increase(by):
# global counter
#
# local_counter = counter
# local_counter += by
#
# sleep(0.1)
#
# counter = local_counter
# print(f'counter={counter}')
#
#
# # create threads
# t1 = Thread(target=increase, args=(8,))
# t2 = Thread(target=increase, args=(20,))
#
# # start the threads
# t1.start()
# t2.start()
#
#
# # wait for the threads to complete
# t1.join()
# t2.join()
#
#
# print(f'The final counter is {counter}')
#