Created
May 24, 2022 12:29
-
-
Save cengizhancaliskan/c112ea95f16a2b1577c3242f836cb4e6 to your computer and use it in GitHub Desktop.
ThreadSafeCounter python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import threading | |
class ThreadSafeCounter: | |
def __init__(self, counter=0, max_counter=0): | |
self.lock = threading.Lock() | |
self.counter = counter | |
self.max_counter = max_counter | |
def get(self): | |
return self.counter | |
def set(self, count): | |
self.counter = count | |
def incr(self, count=1): | |
with self.lock: | |
self.counter += count | |
def get_max(self): | |
return self.max_counter | |
def set_max(self, count): | |
self.max_counter = count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment