forked from mirror/ants
解决竞争锁导致bug
This commit is contained in:
parent
912aa76987
commit
1846b4392a
|
@ -42,9 +42,9 @@ const (
|
||||||
YiB // 1208925819614629174706176
|
YiB // 1208925819614629174706176
|
||||||
)
|
)
|
||||||
const (
|
const (
|
||||||
RunTimes = 10000000
|
RunTimes = 1000000
|
||||||
Param = 100
|
Param = 100
|
||||||
AntsSize = 1000
|
AntsSize = 50000
|
||||||
TestSize = 10000
|
TestSize = 10000
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -68,6 +68,7 @@ func demoPoolFunc(args interface{}) error {
|
||||||
|
|
||||||
func BenchmarkGoroutineWithFunc(b *testing.B) {
|
func BenchmarkGoroutineWithFunc(b *testing.B) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
for j := 0; j < RunTimes; j++ {
|
for j := 0; j < RunTimes; j++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
@ -96,7 +97,7 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) {
|
||||||
p.Serve(Param)
|
p.Serve(Param)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
b.Logf("running goroutines: %d", p.Running())
|
//b.Logf("running goroutines: %d", p.Running())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,7 +112,6 @@ func BenchmarkGoroutine(b *testing.B) {
|
||||||
func BenchmarkAntsPool(b *testing.B) {
|
func BenchmarkAntsPool(b *testing.B) {
|
||||||
p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc)
|
p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc)
|
||||||
defer p.Release()
|
defer p.Release()
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
for j := 0; j < RunTimes; j++ {
|
for j := 0; j < RunTimes; j++ {
|
||||||
|
|
8
pool.go
8
pool.go
|
@ -196,9 +196,17 @@ func (p *Pool) getWorker() *Worker {
|
||||||
}
|
}
|
||||||
|
|
||||||
if waiting {
|
if waiting {
|
||||||
|
for{
|
||||||
p.cond.Wait()
|
p.cond.Wait()
|
||||||
l := len(p.workers) - 1
|
l := len(p.workers) - 1
|
||||||
|
if l < 0{
|
||||||
|
continue
|
||||||
|
}
|
||||||
w = p.workers[l]
|
w = p.workers[l]
|
||||||
|
p.workers[l] = nil
|
||||||
|
p.workers = p.workers[:l]
|
||||||
|
break
|
||||||
|
}
|
||||||
} else if w == nil {
|
} else if w == nil {
|
||||||
w = &Worker{
|
w = &Worker{
|
||||||
pool: p,
|
pool: p,
|
||||||
|
|
|
@ -198,9 +198,18 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
if waiting {
|
if waiting {
|
||||||
|
for{
|
||||||
p.cond.Wait()
|
p.cond.Wait()
|
||||||
l := len(p.workers) - 1
|
l := len(p.workers) - 1
|
||||||
|
if l < 0{
|
||||||
|
continue
|
||||||
|
}
|
||||||
w = p.workers[l]
|
w = p.workers[l]
|
||||||
|
p.workers[l] = nil
|
||||||
|
p.workers = p.workers[:l]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
} else if w == nil {
|
} else if w == nil {
|
||||||
w = &WorkerWithFunc{
|
w = &WorkerWithFunc{
|
||||||
pool: p,
|
pool: p,
|
||||||
|
|
Loading…
Reference in New Issue