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
A figure shows permits as a fixed capacity controlling how many goroutines can access a resource simultaneously. section 5.2.1
