Skip to content

multi_wait

Multi-threaded synchronization barrier with configurable signal count.

This module provides :class:MultiWait, a threading primitive that blocks until a specified number of signals have been received. It is useful for coordinating multiple worker threads that must all reach a checkpoint before proceeding.

MultiWait

A barrier waiting for a configurable number of signals before releasing.

Example
barrier = multi_wait.MultiWait(3)  # released after 3 signal() calls
barrier.signal()
barrier.signal()
barrier.signal()  # all threads waiting in barrier.wait() now proceed
Source code in py_misc_utils/multi_wait.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class MultiWait:
  """A barrier waiting for a configurable number of signals before releasing.

  Example:
    barrier = multi_wait.MultiWait(3)  # released after 3 signal() calls
    barrier.signal()
    barrier.signal()
    barrier.signal()  # all threads waiting in barrier.wait() now proceed
  """

  def __init__(self, count):
    """Initialize the barrier with the required signal count.

    Args:
      count: The number of signals required before waiting threads are released.
    """
    self._count = count
    self._sigcount = 0
    self._lock = threading.Lock()
    self._cond = threading.Condition(lock=self._lock)

  def signal(self, n=1):
    """Signal that n waiters have reached the barrier.

    When the signal count reaches the configured count, all waiting threads
    are released.

    Args:
      n: The number of signals to add (default: 1).
    """
    with self._lock:
      self._sigcount = min(self._sigcount + n, self._count)
      if self._sigcount == self._count:
        self._cond.notify_all()

  def wait(self, timeout=None):
    """Block until the signal count reaches the configured count.

    Args:
      timeout: Optional maximum time in seconds to wait. If None, wait
        indefinitely.

    Returns:
      True if the barrier was released (signal count reached), False if the
      timeout elapsed first.
    """
    with self._lock:
      if self._count > self._sigcount:
        return self._cond.wait(timeout=timeout)

    return True

signal(n=1)

Signal that n waiters have reached the barrier.

When the signal count reaches the configured count, all waiting threads are released.

Parameters:

Name Type Description Default
n

The number of signals to add (default: 1).

1
Source code in py_misc_utils/multi_wait.py
33
34
35
36
37
38
39
40
41
42
43
44
45
def signal(self, n=1):
  """Signal that n waiters have reached the barrier.

  When the signal count reaches the configured count, all waiting threads
  are released.

  Args:
    n: The number of signals to add (default: 1).
  """
  with self._lock:
    self._sigcount = min(self._sigcount + n, self._count)
    if self._sigcount == self._count:
      self._cond.notify_all()

wait(timeout=None)

Block until the signal count reaches the configured count.

Parameters:

Name Type Description Default
timeout

Optional maximum time in seconds to wait. If None, wait indefinitely.

None

Returns:

Type Description

True if the barrier was released (signal count reached), False if the

timeout elapsed first.

Source code in py_misc_utils/multi_wait.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def wait(self, timeout=None):
  """Block until the signal count reaches the configured count.

  Args:
    timeout: Optional maximum time in seconds to wait. If None, wait
      indefinitely.

  Returns:
    True if the barrier was released (signal count reached), False if the
    timeout elapsed first.
  """
  with self._lock:
    if self._count > self._sigcount:
      return self._cond.wait(timeout=timeout)

  return True