-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex_vqueue.go
More file actions
84 lines (71 loc) · 1.29 KB
/
ex_vqueue.go
File metadata and controls
84 lines (71 loc) · 1.29 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/l4go/task"
"github.com/l4go/vqueue"
)
func free(i interface{}) {
v := i.(int)
log.Println("Free:", v)
}
func main() {
signal_ch := make(chan os.Signal, 1)
signal.Notify(signal_ch, syscall.SIGINT, syscall.SIGTERM)
cc := task.NewCancel()
defer cc.Cancel()
go func() {
select {
case <-signal_ch:
cc.Cancel()
case <-cc.RecvCancel():
}
}()
que := vqueue.New(free)
defer que.Close()
log.Printf("start PopOrTimeout()")
res, ok, tout := que.PopOrTimeout(2 * time.Second)
log.Printf("PopOrTimeout(): %v %v %v", res, ok, tout)
do := func(id int) {
for {
res, ok := que.PopWithCancel(cc)
if task.IsCanceled(cc) {
return
}
if !ok {
break
}
log.Printf("Pop(%d): %v", id, res.(int))
free(res)
time.Sleep(500 * time.Millisecond)
}
}
do_tout := func(id int) {
for {
res, ok, _ := que.PopOrTimeout(time.Second)
if task.IsCanceled(cc) {
break
}
if !ok {
break
}
log.Printf("PopOrTimeout(%d): %v", id, res.(int))
free(res)
time.Sleep(500 * time.Millisecond)
}
}
go do(1)
go do(2)
go do_tout(3)
for i := 0; i < 10; i++ {
if task.IsCanceled(cc) {
break
}
que.Push(i)
log.Println("Push:", i)
time.Sleep(100 * time.Millisecond)
}
}