Created
May 10, 2022 11:26
-
-
Save crossphoton/da3ec2d9fb953265d3aa304580355557 to your computer and use it in GitHub Desktop.
Wait Group implementation in 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
| from threading import Lock | |
| class WaitGroup: | |
| def __init__(self): | |
| self.count = 0 | |
| self.complete = Lock() | |
| self.lock = Lock() | |
| def add(self, n): | |
| self.lock.acquire() | |
| self.count += n | |
| self.complete.acquire() | |
| self.lock.release() | |
| def done(self): | |
| self.lock.acquire() | |
| self.count -= 1 | |
| if self.count == 0: | |
| self.complete.release() | |
| self.lock.release() | |
| def wait(self): | |
| self.complete.acquire() | |
| self.complete.release() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment