Created
October 16, 2017 16:30
-
-
Save LuoZijun/87cadb75e3abb97a127e7566fe3021eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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