use WaitGroup to wait all goroutines to exit

This commit is contained in:
Andy Pan 2018-05-19 19:51:33 +08:00
parent 0fd8ba8dae
commit 0423264ffa
4 changed files with 28 additions and 20 deletions

View File

@ -22,6 +22,6 @@ func Free() int {
return defaultPool.Free() return defaultPool.Free()
} }
//func Wait() { func Wait() {
// defaultPool.Wait() defaultPool.Wait()
//} }

View File

@ -3,16 +3,18 @@ package ants_test
import ( import (
"testing" "testing"
"github.com/panjf2000/ants" "github.com/panjf2000/ants"
"fmt" "sync"
"runtime"
) )
var n = 100000 var n = 100000
func demoFunc() { func demoFunc() {
var n int
for i := 0; i < 1000000; i++ { for i := 0; i < 1000000; i++ {
n += i
} }
} }
//func demoFunc() { //func demoFunc() {
// var n int // var n int
// for i := 0; i < 10000; i++ { // for i := 0; i < 10000; i++ {
@ -30,11 +32,11 @@ func TestDefaultPool(t *testing.T) {
t.Logf("running workers number:%d", ants.Running()) t.Logf("running workers number:%d", ants.Running())
t.Logf("free workers number:%d", ants.Free()) t.Logf("free workers number:%d", ants.Free())
//ants.Wait() ants.Wait()
mem := runtime.MemStats{} //mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) //runtime.ReadMemStats(&mem)
fmt.Println("memory usage:", mem.TotalAlloc/1024) //fmt.Println("memory usage:", mem.TotalAlloc/1024)
} }
//func TestCustomPool(t *testing.T) { //func TestCustomPool(t *testing.T) {
@ -53,11 +55,17 @@ func TestDefaultPool(t *testing.T) {
//} //}
func TestNoPool(t *testing.T) { func TestNoPool(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
go demoFunc() wg.Add(1)
go func() {
defer wg.Done()
demoFunc()
}()
} }
mem := runtime.MemStats{} wg.Wait()
runtime.ReadMemStats(&mem) //mem := runtime.MemStats{}
fmt.Println("memory usage:", mem.TotalAlloc/1024) //runtime.ReadMemStats(&mem)
//fmt.Println("memory usage:", mem.TotalAlloc/1024)
} }

12
pool.go
View File

@ -20,7 +20,7 @@ type Pool struct {
launchSignal chan sig launchSignal chan sig
destroy chan sig destroy chan sig
m *sync.Mutex m *sync.Mutex
//wg *sync.WaitGroup wg *sync.WaitGroup
} }
func NewPool(size int) *Pool { func NewPool(size int) *Pool {
@ -31,7 +31,7 @@ func NewPool(size int) *Pool {
freeSignal: make(chan sig, math.MaxInt32), freeSignal: make(chan sig, math.MaxInt32),
launchSignal: make(chan sig, math.MaxInt32), launchSignal: make(chan sig, math.MaxInt32),
destroy: make(chan sig, runtime.GOMAXPROCS(-1)), destroy: make(chan sig, runtime.GOMAXPROCS(-1)),
//wg: &sync.WaitGroup{}, wg: &sync.WaitGroup{},
} }
p.loop() p.loop()
return p return p
@ -59,8 +59,8 @@ func (p *Pool) Push(task f) error {
return nil return nil
} }
p.tasks.push(task) p.tasks.push(task)
p.wg.Add(1)
p.launchSignal <- sig{} p.launchSignal <- sig{}
//p.wg.Add(1)
return nil return nil
} }
func (p *Pool) Running() int { func (p *Pool) Running() int {
@ -75,9 +75,9 @@ func (p *Pool) Cap() int {
return int(atomic.LoadInt32(&p.capacity)) return int(atomic.LoadInt32(&p.capacity))
} }
//func (p *Pool) Wait() { func (p *Pool) Wait() {
// p.wg.Wait() p.wg.Wait()
//} }
func (p *Pool) Destroy() error { func (p *Pool) Destroy() error {
p.m.Lock() p.m.Lock()

View File

@ -19,7 +19,7 @@ func (w *Worker) run() {
case f := <-w.task: case f := <-w.task:
f() f()
w.pool.workers.push(w) w.pool.workers.push(w)
//w.pool.wg.Done() w.pool.wg.Done()
case <-w.exit: case <-w.exit:
atomic.AddInt32(&w.pool.running, -1) atomic.AddInt32(&w.pool.running, -1)
return return