-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
59 lines (51 loc) · 953 Bytes
/
pool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package g
import (
"fmt"
"sync"
"sync/atomic"
"testing"
)
func TestNewGoPool(t *testing.T) {
fmt.Println(1 << 16)
p := NewGoPool(nil, 1<<16).
Start()
// When the coroutine pool exits, customize the to-do task list, If not set, all pending tasks will be discarded.
p.Clean(func(work func()) { work() })
all := int32(0)
{
wg := sync.WaitGroup{}
for i := 0; i < 500; i++ {
wg.Add(1)
go func(a int) {
defer wg.Done()
for j := 0; j < 10000; j++ {
p.Submit(func() {
// fmt.Println("first", a)
atomic.AddInt32(&all, 1)
})
}
}(i)
}
wg.Wait()
p.Stop(nil).Wait()
}
{
wg := sync.WaitGroup{}
p.Start()
for i := 0; i < 500; i++ {
wg.Add(1)
go func(a int) {
defer wg.Done()
for j := 0; j < 10000; j++ {
p.Submit(func() {
// fmt.Println("twice", a)
atomic.AddInt32(&all, 1)
})
}
}(i)
}
wg.Wait()
p.Stop(nil).Wait()
}
fmt.Println(all)
}