Skip to content

Instantly share code, notes, and snippets.

@crossphoton
Created May 10, 2022 11:26
Show Gist options
  • Select an option

  • Save crossphoton/da3ec2d9fb953265d3aa304580355557 to your computer and use it in GitHub Desktop.

Select an option

Save crossphoton/da3ec2d9fb953265d3aa304580355557 to your computer and use it in GitHub Desktop.
Wait Group implementation in Python
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