There are in sync goroutine golang in 2 ways, either using a channel or use sync.WaitGroup, this article is how to synchronize goroutine through the channel. Look at the code.
1 package main
2
3 import (
4 "os"
5 "os/signal"
6 "runtime"
7 "log"
8 "syscall"
9 )
10
11 const NUM_OF_QUIT int = 100
12
13 func main() {
14 runtime.GOMAXPROCS(runtime.NumCPU())
15 done := make(chan bool)
16 receive_channel := make(chan chan bool)
17 finish := make(chan bool)
18
19
20 for i := 0; i < NUM_OF_QUIT; i++ {
21 go do_while_select(i, receive_channel, finish)
22 }
23
24 go handle_exit(done, receive_channel, finish)
25
26 <-done
27 os.Exit(0)
28
29 }
30 func handle_exit(done chan bool, receive_channel chan chan bool, finish chan bool) {
31 sigs := make(chan os.Signal, 1)
32 signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
33 chan_slice := make([]chan bool, 0)
34 for {
35 select {
36 case <-sigs:
37 for _, v := range chan_slice {
38 v <- true
39 }
40 for i := 0; i < len(chan_slice); i++ {
41 <-finish
42 }
43 done <- true
44 runtime.Goexit()
45 case single_chan := <-receive_channel:
46 log.Println("the single_chan is ", single_chan)
47 chan_slice = append(chan_slice, single_chan)
48 }
49 }
50 }
51 func do_while_select(num int, rece chan chan bool, done chan bool) {
52 quit := make(chan bool)
53 rece <- quit
54 for {
55 select {
56 case <-quit:
57 done <- true
58 runtime.Goexit()
59 default:
60 //简单输出
61 log.Println("the ", num, "is running")
62 }
63 }
64 }
The above code is saved as example.go, by gotool compile the code:
go build example.go
There are example files in the current directory, run the file in the terminal
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
2013/03/19 21:17:14 the 0 is running
......
Digital output continuously above goroutine in, waiting for exit signals.
Open a new terminal, find the process name by ps, kill the process via the kill tool:
$ps aux | grep example
user 4026 77.9 0.0 39436 1716 pts/1 Sl+ 21:19 0:17 ./example
$kill 4026
Soon you can see the print in the first terminal no longer there, bringing the presentation is completed.
Code thoughts:
New NUM_OF_QUIT a goroutine, these goroutine inside a newly built one chan bool, through this channel to receive signals exit, these channel in the new time, have been sent to the handle_exit. In this handle_exit goroutine which, triggered by the system monitoring section 1 over the exit signal, and then notifies the other goroutin exit gracefully; goroutine collect other hand sent, through the channel slice. handle_exit inform other goroutine elegant and exit, and then send a signal to the main initiative to withdraw from the process.
You can modify NUM_OF_QUIT value, for example instead 10000, this time, kill command is issued to, to wait quite a long time to see the first terminal to stop printing.
reference:
Go by Example: Signals
https://gobyexample.com/signals
Posted please specify from: Advisa
Reproduced in: https: //my.oschina.net/u/191928/blog/618675