This is the 15th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

In my column:Let’s Golang

“Let’s Golang together” makes the coroutine kill itself

In this blog post, we discuss how Go coroutines kill their own coroutines. Here we need runtime.goexit ().

Runtime.goexit () and how to use it

package main
​
import (
    "fmt"
    "runtime"
    "time"
)
​
func task051(a)  {
    defer fmt.Println("Bring it to you.")
​
    fmt.Println("Song to the sky.")
    fmt.Println("White fur floats in green water.")
    // Kill the current coroutine
    // Goexit terminates the goroutine that calls it. No other goroutine is affected.
    // Goexit runs all deferred calls before terminating the goroutine. Because Goexit
    // is not a panic, any recover calls in those deferred functions will return nil.
    //
    // Calling Goexit from the main goroutine terminates that goroutine
    // without func main returning. Since func main has not returned,
    // the program continues execution of other goroutines.
    // If all other goroutines exit, the program crashes.
    runtime.Goexit()
​
    fmt.Println("Anthurium to clear waves.")}func main(a) {
    go func(a) {
        fmt.Println(Goose, goose, goose.)
        task051()
        fmt.Println("-- King Lubin")
    }()
    // Sleep pauses the current goroutine for at least the duration d.
    // A negative or zero duration causes Sleep to return immediately.
    time.Sleep(time.Second)
}
​
Copy the code

If Goexit kills its goroutine, no other goroutine is affected. Goexit calls all of the delay functions before terminating the Goroutine, because Goexit is not a panic, and any of these delay functions call recovery will return nil. Calling Goexit from the main coroutine terminates the main coroutine and does not return func main. Since func main does not return, the program continues with other goroutines. If all other Goroutines terminate, then the program crashes.

In this code, the main function is to open up a coroutine, the first output “xu Goose” the first line of poetry, and then into the task function. The task function is executed in a subcoroutine.

The result of running this code is

Goose, goose, goose sing to the sky, white fur floating, green water bring youCopy the code

There is no output for “Antares purge” because it comes after runtime.goexit (), by which time the coroutine has already been killed by itself.

But the delay functions are executed, and Goexit will call all the delay functions before terminating the Goroutine, because Goexit is not a panic, and any call to restore in these delay functions will return nil. So “Bring it to you” doesn’t print.

But the author’s name “- King Luobin” why not output? Think about it.

Task051 () kills the current coroutine. The author’s name “– Luobin Wang” cannot be executed because the coroutine has been killed.

The first one killed was a subcoroutine.

We said the main coroutine can’t die, so let’s kill the main coroutine and see what happens!

When the main coroutine is killed, all the subcoroutines are messed up and can’t sleep.

Let’s first see what the main coroutine looks like when it ends properly…

func main(a) {
    go task061()
    // The main coroutine sleeps for 5 seconds
    time.Sleep(5 * time.Second)
​
    //runtime.Goexit()
}
func task061(a){
    for{
        fmt.Println("Mission in progress...")
        time.Sleep(time.Second)
    }
}
Copy the code

The result of this operation is:

Mission in progress... Mission in progress... Mission in progress... Mission in progress... Mission in progress...Copy the code

The main coroutine sleeps for 5 seconds, and the subcoroutine says “task in progress…” without a second’s sleep. , so when the main coroutine ends, the main coroutine says five sentences “task in progress…” .

Now let’s kill the main coroutine and see what happens!

Activate runtime.goexit () of the above code block so that it can run.

Look at the results.

Mission in progress... Mission in progress... . In the process of...Copy the code

The blogger waited half a minute while the subcoroutine shouted “Mission in progress…” “Like a screaming child when his parents are not home at night.