Reputation: 1721
I have two goroutine like,
Routine 1 {
// do something
}
Routine 2 {
// do something
}
main {
// do something
}
Is it possible from in routine 1, if some condition met, stop whole program execution ? Stop execution of main and Routine 2 ? Can give a simple example.
Upvotes: 2
Views: 2633
Reputation: 2489
You could also use a channel to have routine1 communicate with routine2. WLOG routine1 could send something down the channel and routine2 could use a select statement to either take something off of that channel or something off of another "work" channel (a channel that provides work to the routine). When routine2 takes something off of the "kill execution" channel, it could finish up and call os.Exit(0).
Upvotes: 2
Reputation: 166569
For example,
package main
import "os"
func routine1() {
// set exit = true when ready to exit
exit := false
if exit {
os.Exit(0)
}
}
func routine2() {
}
func main() {
go routine1()
go routine2()
}
Upvotes: 4