-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
48 lines (37 loc) · 784 Bytes
/
example_test.go
File metadata and controls
48 lines (37 loc) · 784 Bytes
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
package gopool_test
import (
"context"
"fmt"
"github.com/codegrapple/gopool"
)
func ExamplePool_basic() {
ctx := context.Background()
// Pool with bounded parallelism.
pool := gopool.New(2, 8)
defer pool.Shutdown(ctx)
// Two independent handles, each bound to a single goroutine.
h1 := pool.NewHandle()
h2 := pool.NewHandle()
var (
seq1, seq2 int
out1, out2 []int
)
// Schedule work concurrently, but observe sequential execution per handle.
for i := 0; i < 3; i++ {
h1.Schedule(ctx, func() {
out1 = append(out1, seq1)
seq1++
})
h2.Schedule(ctx, func() {
out2 = append(out2, seq2)
seq2++
})
}
// Ensure all scheduled work has completed.
pool.Shutdown(ctx)
fmt.Println(out1)
fmt.Println(out2)
// Output:
// [0 1 2]
// [0 1 2]
}