Ai
1 Star 0 Fork 0

shifengbin/concurrent

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
controller.go 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
shifengbin 提交于 2022-01-29 18:01 +08:00 . 并发控制
package concurrent
import (
"log"
"sync"
)
//WorkerController 并发控制器
type WorkerController struct {
max int //最大数量
current int //当前数量
cond *sync.Cond
}
//add 通知制器增加添加一个正在工作的worker, 超过最大数量会等待
func (w *WorkerController) add() {
w.cond.L.Lock()
w.current += 1
for w.current > w.max {
w.cond.Wait()
}
w.cond.L.Unlock()
}
//done 通知控制一个worker已完成
func (w *WorkerController) done() {
w.cond.L.Lock()
w.current -= 1
w.cond.L.Unlock()
w.cond.Broadcast()
}
//Wait 等待所有worker完成
func (w *WorkerController) Wait() {
w.cond.L.Lock()
for w.current > 0 {
w.cond.Wait()
}
w.cond.L.Unlock()
}
//Go 开启一个worker
func (w *WorkerController) Go(f func()) {
w.add()
go func() {
defer func() {
if err := recover(); err != nil {
log.Println("panic", err)
}
w.done()
}()
f()
}()
}
//NewWorkerController 创建一个控制器
func NewWorkerController(max int) *WorkerController {
return &WorkerController{
max: max,
cond: sync.NewCond(&sync.Mutex{}),
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/shifengbin/concurrent.git
git@gitee.com:shifengbin/concurrent.git
shifengbin
concurrent
concurrent
4c90254c9e94

搜索帮助