-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutexRoutines.go
62 lines (57 loc) · 1.12 KB
/
mutexRoutines.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
60
61
62
package main
import (
"fmt"
"log"
"runtime"
"sync/atomic"
"time"
)
func foo(retry *int32) error {
log.Println("inside foo1: ", *retry)
atomic.AddInt32(retry, 1)
//log.Println("inside foo2: ", *retry)
time.Sleep(3 * time.Second)
return fmt.Errorf("error from foo")
}
func bar(retry *int32) error {
log.Println("inside bar1: ", *retry)
atomic.AddInt32(retry, 1)
//log.Println("inside bar2: ", *retry)
time.Sleep(5 * time.Second)
return fmt.Errorf("error from bar")
}
func main() {
log.Println(runtime.NumGoroutine())
var retry int32
errChan := make(chan error)
for retry < 4 {
//log.Println("1: ", retry)
go func() {
errChan <- foo(&retry)
}()
go func() {
errChan <- bar(&retry)
}()
log.Printf("+++ %v", <-errChan)
go func() {
drainChan(errChan)
}()
//log.Println("2: ", retry)
time.Sleep(1 * time.Second)
}
time.Sleep(8 * time.Second)
log.Println(runtime.NumGoroutine())
}
func drainChan(err <-chan error) {
for e := range err {
log.Printf("====== draining %v", e)
return
}
//for {
// select {
// case e := <-err:
// log.Printf("====== draining %v", e)
// return
// }
//}
}