Harshit2N commited on
Commit
6ad51b0
·
unverified ·
1 Parent(s): a21d9ae

Update counter.py

Browse files
Files changed (1) hide show
  1. data/contexts/counter.py +11 -9
data/contexts/counter.py CHANGED
@@ -3,16 +3,18 @@ import threading
3
  class Counter:
4
  def __init__(self):
5
  self.count = 0
 
6
 
7
  def increment(self):
8
- current = self.count
9
- self.count = current + 1
10
- return self.count
11
-
12
- def get_count(self):
13
- return self.count
14
 
15
  def decrement(self):
16
- current = self.count
17
- self.count = current - 1
18
- return self.count
 
 
 
 
 
3
  class Counter:
4
  def __init__(self):
5
  self.count = 0
6
+ self.lock = threading.Lock()
7
 
8
  def increment(self):
9
+ with self.lock:
10
+ self.count += 1
11
+ return self.count
 
 
 
12
 
13
  def decrement(self):
14
+ with self.lock:
15
+ self.count -= 1
16
+ return self.count
17
+
18
+ def get_count(self):
19
+ with self.lock:
20
+ return self.count