15 Star 55 Fork 14

bits-chen / go-goes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
worker.go 758 Bytes
一键复制 编辑 原始数据 按行查看 历史
yoojia.chen 提交于 2018-07-19 12:59 . Update
package goes
//
// Author: 陈永佳 chenyongjia@parkingwang.com, yoojiachen@gmail.com
//
type GoTask func() // 任务
// Worker包含一个任务列表和停止控制信号
type worker struct {
tasks chan GoTask // 任务列表
waitExit chan struct{} // 运行状态
}
// 启动内部协程,异步循环处理任务列表
func (slf *worker) work(idles chan<- *worker) {
defer close(slf.waitExit)
finishTask := func() {
idles <- slf
}
for taskFunc := range slf.tasks {
func() {
// 完成一个任务后,将当前Worker恢复到空闲状态
defer finishTask()
taskFunc()
}()
}
}
func newWorker() *worker {
return &worker{
tasks: make(chan GoTask, 1), // 1: for sync send task
waitExit: make(chan struct{}),
}
}
Go
1
https://gitee.com/bitschen/go-goes.git
git@gitee.com:bitschen/go-goes.git
bitschen
go-goes
go-goes
master

搜索帮助