Go Cond: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 24: Line 24:
...
...
c.L.Unlock()
c.L.Unlock()
// Ensure that other goroutine competing for the condition are woken up with Broadcast() or Signal()
</syntaxhighlight>
</syntaxhighlight>



Revision as of 23:56, 20 January 2024

External

Internal

Overview

Cond implements a condition variable, a rendezvous point for goroutines waiting for or announcing the occurrence of an event. In this definition, an "event" is an arbitrary signal between two or more goroutines that carries no information other than the fact that it has occurred. Cond offers a way for a goroutine to "efficiently sleep" (be suspended) until it is signaled to wake up and check of a condition, by other calling Signal() or Broadcast().

For many simple use cases, users will be better off using channels than a Cond (Broadcast() corresponds to closing a channel, and Signal corresponds to sending on a channel).

The typical usage pattern is described below:

Each goroutine that wants to access condition, concurrently, uses this access pattern:

cond.L.Lock()
for !condition() {
  c.Wait() // Wait() releases the lock internally, as a side effect
}

// At this point the goroutine has exclusive access to the condition.

// Make use of the condition, accessing it is concurrently-safe.
...
c.L.Unlock()
// Ensure that other goroutine competing for the condition are woken up with Broadcast() or Signal()

To wake just one goroutine waiting on the condition use Signal(). To wake up all goroutines waiting on the condition use Broadcast().

Each Cond has an associated Locker L, commonly a *Mutex or *RWMutex, which must be held when changing the condition and when calling the Wait method. A Cond must not be copied after first use. In the terminology of the Go memory model, Cond arranges that a call to Broadcast() or Signal() "synchronizes before" any Wait call that it unblocks.