Home  >  Article  >  Backend Development  >  How to close goroutine in golang

How to close goroutine in golang

PHPz
PHPzOriginal
2023-04-14 14:34:32587browse

Golang is a very popular programming language with the characteristics of efficiency, scalability and reliability. In Golang, goroutine is one of the very important concepts. Goroutines are lightweight threads that allow multiple functions to run simultaneously in the same process. When writing Golang programs, closing goroutine is a very important issue. This article will introduce how to close goroutine.

  1. Use chan and bool type variables to close goroutine

In Golang, you can use channel to manage goroutine. We can create a bool type variable done and include it in the channel. In goroutine, we can use the select statement to listen to the channel. As soon as the channel is closed, the goroutine will stop running.

The following is a sample code:

func worker(c <-chan bool) {
    for {
        select {
        case <-c:
            return
        default:
            // do some work
        }
    }
}

func main() {
    c := make(chan bool)
    go worker(c)

    // some other work

    close(c)
}

In this sample code, we first create a channel c. Then we use the go keyword to create a goroutine that calls the worker function and passes c as parameter. In the main function, we perform some other work and finally call close(c) to close the goroutine. In the worker function, we use the select statement to listen to the channel. As soon as the channel is closed, the goroutine will stop running.

  1. Use the context package to shut down goroutine

Golang's context package provides an elegant way to shut down goroutine. Context is a cancelable context that provides values ​​and methods that can be passed to the goroutine to shut down the goroutine. Once the context is canceled, all goroutines derived from this context will be canceled.

The following is a sample code:

func worker(ctx context.Context) {
    for {
        select {
        case <-ctx.Done():
            return
        default:
            // do some work
        }
    }
}

func main() {
    ctx, cancel := context.WithCancel(context.Background())
    go worker(ctx)

    // some other work

    cancel()
}

In this sample code, we first create a cancellation context ctx and use the context.WithCancel function to convert it into a cancelable context. Then we use the go keyword to create a goroutine that calls the worker function and passes ctx as parameter. In the main function, we perform some other work and finally call the cancel() function to cancel the goroutine. In the worker function, we use the select statement to listen to the context. Once the context is canceled, the goroutine will stop running.

Summary

Close goroutine is a very important issue in writing Golang programs. In this article, we introduced two ways to close goroutine, namely using chan and bool type variables and using the context package. With good goroutine management, we can write efficient, scalable and reliable Golang programs.

The above is the detailed content of How to close goroutine in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn