Skip to content

Instantly share code, notes, and snippets.

@LuoZijun
Created October 16, 2017 16:30
Show Gist options
  • Save LuoZijun/87cadb75e3abb97a127e7566fe3021eb to your computer and use it in GitHub Desktop.
Save LuoZijun/87cadb75e3abb97a127e7566fe3021eb to your computer and use it in GitHub Desktop.
package main
import "os";
import "syscall";
import "fmt";
import "os/signal";
var GLOBAL_SIGNAL := make(chan bool, 1);
func is_alive () bool {
state := <- GLOBAL_SIGNAL;
GLOBAL_SIGNAL <- state
return state;
}
func job (num int){
fmt.Printfln("Job %v: start ...", num);
for {
if !is_alive() {
break;
}
// do something
}
fmt.Printfln("Job %v: stop ...", num);
}
func main (){
var c := make(chan os.Signal, 1);
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT);
go func(){
GLOBAL_SIGNAL <- true
_ := <- c
GLOBAL_SIGNAL <- false
}();
go job(1);
go job(2);
go job(3);
}
package main
import "os";
import "syscall";
import "fmt";
import "os/signal";
var GLOBAL_SIGNAL := 0;
func is_alive () bool {
return GLOBAL_SIGNAL == 0;
}
func job (num int) {
fmt.Printfln("Job %v: start ...", num);
for {
if !is_alive() {
break;
}
// do something
}
fmt.Printfln("Job %v: stop ...", num);
}
func main (){
var c := make(chan os.Signal, 1);
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT);
go func(){
_ := <- c
GLOBAL_SIGNAL = 1;
}();
go job(1);
go job(2);
go job(3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment