zor*_*red -2 design-patterns go
在 Go 中有一个缓冲通道的概念。这是一个通道,在您填充其缓冲区之前不会被阻塞。
一般缓冲锁定是否有任何一般模式?它将为有限数量的客户端锁定一些资源。
为有限数量的客户端锁定某些资源的原语称为信号量。
使用缓冲通道很容易实现:
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...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
68 次 |
最近记录: |