Spaces:
Sleeping
Sleeping
Harshit2N commited on
Update counter.py
Browse files- 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 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def get_count(self):
|
| 13 |
-
return self.count
|
| 14 |
|
| 15 |
def decrement(self):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|