Documentation
¶
Overview ¶
Package sema provides a simple semaphore implementation to control concurrency in Go programs. A semaphore is a synchronization primitive that limits the number of goroutines that can run concurrently, ensuring that resources (e.g., CPU, memory, I/O) are not overwhelmed by too many simultaneous tasks. This package is useful for scenarios like parallel file processing, API rate limiting, or managing worker pools.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Semaphore ¶
type Semaphore interface {
// Acquire takes a slot in the semaphore, blocking if no slots are available until one is freed.
// This method should be called before starting a concurrent task.
Acquire()
// Release frees a slot in the semaphore, allowing another goroutine to acquire it.
// This method should be called when a concurrent task is complete.
Release()
// Len returns the current number of occupied slots in the semaphore.
// It indicates how many goroutines are currently active.
Len() int
// IsEmpty returns true if no slots are occupied (i.e., no goroutines are active).
IsEmpty() bool
}
Semaphore defines the interface for a semaphore that controls concurrent access. It provides methods to acquire and release slots, check the current usage, and determine if the semaphore is empty.
Click to show internal directories.
Click to hide internal directories.