liveBook · Manning

Permit

As explained in Learn Concurrent Programming with Go

A semaphore permit is a counted unit of availability that an acquisition can consume and a release can add.

A permit is one counted unit of availability in a semaphore. Acquiring consumes one permit; releasing adds one back. When no permits remain, an acquiring goroutine waits until another goroutine releases one. section 5.2.1

Permits support two closely related concurrency patterns:

  • Limit access to a shared resource, such as allowing only a fixed number of goroutines to use a slow database concurrently. section 5.2.1
  • Represent completion with a semaphore initialized to zero: Release records completed work, while Acquire waits for or consumes that recorded signal. section 5.2.3

Waiting for a goroutine to finish

Create a semaphore with zero permits, start doWork, and have doWork call Release when finished. The main goroutine calls Acquire; it blocks if work is unfinished and returns immediately if the release happened first. A 50,000-iteration Go listing demonstrates this pattern. section 5.2.3

Figure 5.11 A fixed number of goroutines are allowed to have access.

A figure shows permits as a fixed capacity controlling how many goroutines can access a resource simultaneously. section 5.2.1

How Learn Concurrent Programming with Go covers it

How deep it goes
Explains permit counts, acquisition, release, blocking, and zero-permit signaling, but does not develop implementation internals.
Examples
Uses a slow database limit, a zero-permit completion signal, and a Go loop repeating the pattern 50,000 times.
What it assumes
Assumes goroutines and shared-resource access while introducing semaphore operations alongside condition-variable signaling.
What you can do after
Apply semaphores to bound goroutine access or wait reliably for concurrent work to complete.
How it connects
Contrasts permits with mutexes and connects them to semaphores, condition variables, goroutines, and shared resources.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.