-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (57 loc) · 2.19 KB
/
main.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
package main
import (
"fmt"
)
func printEven(evenChan, oddChan chan bool) {
for i := 0; i <= 10; i += 2 {
<-evenChan // Wait for signal from main or odd goroutine
fmt.Println(i, "Even") // Print even number
oddChan <- true // Signal odd goroutine
}
}
func printOdd(oddChan, evenChan chan bool, doneChan chan bool) {
for i := 1; i <= 9; i += 2 {
<-oddChan // Wait for signal from even goroutine
fmt.Println(i, "Odd") // Print odd number
evenChan <- true // Signal even goroutine
}
<-oddChan // Wait for the last signal from even goroutine
doneChan <- true // Signal main function that printing is done
}
func main() {
evenChan := make(chan bool)
oddChan := make(chan bool)
doneChan := make(chan bool)
go printEven(evenChan, oddChan)
go printOdd(oddChan, evenChan, doneChan)
evenChan <- true // Start the sequence by signaling even goroutine
<-doneChan // Wait for the completion signal
}
/*output steps
evenChan kick start the sequence by sending a signal
printEven start execution and print 0
send signal to oddChan to start printing odd numbers
printOdd start execution and print 1
send signal to evenChan to print next even number
printEven start execution and print 2
send signal to oddChan to print next odd number
printOdd start execution and print 3
send signal to evenChan to print next even number
printEven start execution and print 4
send signal to oddChan to print next odd number
printOdd start execution and print 5
send signal to evenChan to print next even number
printEven start execution and print 6
send signal to oddChan to print next odd number
printOdd start execution and print 7
send signal to evenChan to print next even number
printEven start execution and print 8
send signal to oddChan to print next odd number
printOdd start execution and print 9
send signal to evenChan to print next even number
printEven start execution and print 10
send signal to oddChan to print next odd number
Here printOdd will not print 11 because the loop condition is i <= 9
it will still empty the oddChan and evenChan will not get signal to print next even number
main function will get the doneChan signal and print "done"
*/