Home >Backend Development >Golang >Can Semaphores Emulate Buffered Locking in Go?
Buffered Lock Patterns
In Go, the concept of a buffered channel enables non-blocking operations until the channel's buffer is filled. Inspired by this mechanism, the inquiry arises: is there a generalized pattern akin to buffered channels for "buffered locking," where a resource can be locked for a finite set of clients?
Answer: Semaphores
The primitive that fulfills this requirement is a semaphore. Constructed with a buffered channel, a semaphore imposes a limit on the number of concurrent clients that can access a resource.
Consider the following implementation using a buffered channel:
var semaphore = make(chan struct{}, 4) // allow four concurrent users func f() { // Grab the lock. Blocks as long as 4 other invocations of f are still running. semaphore <- struct{}{} // Release the lock once we're done. defer func() { <-semaphore }() // Do work... }
In this scenario, the f function acquires the lock by sending a value into the semaphore channel. If the channel is full, representing the maximum allowed concurrency, f blocks until another client releases the lock by receiving a value from the channel. The defer statement ensures that the lock is released when the function returns.
The above is the detailed content of Can Semaphores Emulate Buffered Locking in Go?. For more information, please follow other related articles on the PHP Chinese website!