Merge branch 'develop'

This commit is contained in:
andy pan 2018-06-15 11:27:00 +08:00
commit b92dc131a8
2 changed files with 11 additions and 17 deletions

View File

@ -34,8 +34,8 @@ import (
var sum int32 var sum int32
func myFunc(i interface{}) error { func myFunc(i interface{}) error {
n := i.(int) n := i.(int32)
atomic.AddInt32(&sum, int32(n)) atomic.AddInt32(&sum, n)
fmt.Printf("run with %d\n", n) fmt.Printf("run with %d\n", n)
return nil return nil
} }
@ -73,7 +73,7 @@ func main() {
// submit tasks // submit tasks
for i := 0; i < runTimes; i++ { for i := 0; i < runTimes; i++ {
wg.Add(1) wg.Add(1)
p.Serve(i) p.Serve(int32(i))
} }
wg.Wait() wg.Wait()
fmt.Printf("running goroutines: %d\n", p.Running()) fmt.Printf("running goroutines: %d\n", p.Running())

22
pool.go
View File

@ -140,6 +140,7 @@ func (p *Pool) getWorker() *Worker {
p.running++ p.running++
} }
} else { } else {
<-p.freeSignal
w = workers[n] w = workers[n]
workers[n] = nil workers[n] = nil
p.workers = workers[:n] p.workers = workers[:n]
@ -148,20 +149,13 @@ func (p *Pool) getWorker() *Worker {
if waiting { if waiting {
<-p.freeSignal <-p.freeSignal
for { p.lock.Lock()
p.lock.Lock() workers = p.workers
workers = p.workers l := len(workers) - 1
l := len(workers) - 1 w = workers[l]
if l < 0 { workers[l] = nil
p.lock.Unlock() p.workers = workers[:l]
continue p.lock.Unlock()
}
w = workers[l]
workers[l] = nil
p.workers = workers[:l]
p.lock.Unlock()
break
}
} else if w == nil { } else if w == nil {
w = &Worker{ w = &Worker{
pool: p, pool: p,