l-export-async/coroutine/fixed.go

24 lines
426 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package coroutine
type Fixed struct {
queues chan struct{}
}
func NewFixed(count int) *Fixed {
return &Fixed{
queues: make(chan struct{}, count),
}
}
// Run 运行指定函数
// name 协程名称
// fn 协程执行的函数
func (f *Fixed) Run(name string, fn func()) {
f.queues <- struct{}{}
runAfter(name, fn, func() {
<-f.queues
})
}
// chan 的发送次数 >= 接收次数可以不用close由GC回收