Skip to content

Commit

Permalink
feat: native pool
Browse files Browse the repository at this point in the history
  • Loading branch information
miaoyin committed May 15, 2024
1 parent 69667ca commit 1ac7230
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pools/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (p *Native[I, O]) Post(elems ...I) []O {
return nil
}

var wait sync.WaitGroup
wait := &sync.WaitGroup{}

wait.Add(length)

Expand All @@ -36,16 +36,20 @@ func (p *Native[I, O]) Post(elems ...I) []O {
for idx, elem := range elems {
p.group.Incr()

go func(num int, item I, list []O) {
list[num] = p.yield(item, num)
}(idx, elem, ret)
go p.run(idx, elem, ret, wait)
}

wait.Wait()

return ret
}

func (p *Native[I, O]) run(idx int, elem I, list []O, wait *sync.WaitGroup) {
list[idx] = p.yield(elem, idx)

wait.Done()
}

// Close 关闭协程池.
func (p *Native[I, O]) Close() {
p.group.Wait()
Expand Down

0 comments on commit 1ac7230

Please sign in to comment.