-
Notifications
You must be signed in to change notification settings - Fork 79
/
sequencing.go
80 lines (60 loc) · 1.92 KB
/
sequencing.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Restoring Sequencing
Send a channel on a channel, making Goroutine wait its turn
Receive all messages, then enable them again by sending on a private channel
Each speaker must wait for a go-ahead.
Joe: Send and Wait --- --- Joe: Display and Wait
\ /
----- FanIn ---
/ \
Ann: Send and Wait --- --- Ann: Display then Release Waits
*/
package main
import (
"fmt"
"math/rand"
"time"
)
// Message contains a channel for the reply.
type Message struct {
str string
wait chan bool // Acts as a signaler
}
func main() {
c := fanIn(boring("Joe"), boring("Ann"))
for i := 0; i < 10; i++ {
msg1 := <-c // Waiting on someone (Joe) to talk
fmt.Println(msg1.str)
msg2 := <-c // Waiting on someone (Ann) to talk
fmt.Println(msg2.str)
msg1.wait <- true // Joe can run again
msg2.wait <- true // Ann can run again
}
fmt.Println("You're boring: I'm leaving.")
}
func fanIn(input1, input2 <-chan Message) <-chan Message {
c := make(chan Message) // The FanIn channel.
go func() { // This Goroutine will receive messages from Joe.
for {
c <- <-input1 // Write the message to the FanIn channel, Blocking Call.
}
}()
go func() { // This Goroutine will receive messages from Ann.
for {
c <- <-input2 // Write the message to the FanIn channel, Blocking Call.
}
}()
return c
}
func boring(msg string) <-chan Message { // Returns receive-only (<-) channel of strings.
c := make(chan Message)
waitForIt := make(chan bool) // Give main control over our execution.
go func() { // Launch the goroutine from inside the function. Function Literal.
for i := 0; ; i++ {
c <- Message{fmt.Sprintf("%s %d", msg, i), waitForIt}
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
<-waitForIt // Block until main tells us to go again.
}
}()
return c // Return the channel to the caller.
}