Skip to content

Commit

Permalink
Create Race Condition with Solution.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Isha307 authored Jun 30, 2022
1 parent 61f885f commit fa4259b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions OS/Race Condition/Race Condition with Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from threading import Thread, Lock
import time


counter = 0
def increase_counter(x,lock):
lock.acquire()
global counter
cnt = counter
cnt += x
time.sleep(0.5)
counter = cnt
lock.release()

if __name__ == "__main__":
lock = Lock()
t1 = Thread(target=increase_counter, args=(20,lock ))
t2 = Thread(target=increase_counter, args=(10,lock ))
t1.start()
t2.start()
t1.join()
t2.join()
print(counter)

0 comments on commit fa4259b

Please sign in to comment.