Decryption: The waiting strategy of the main function in Go language requires specific code examples
Go language is a concurrent programming language, and its waiting strategy of the main function is particularly important . The main function needs to ensure that it exits after all goroutines have been executed, otherwise it may cause the program to terminate prematurely. This article will introduce several common main function waiting strategies and provide specific code examples.
In the Go language, WaitGroup or channel in the sync package is usually used to implement the waiting of the main function. Below we will introduce the specific applications of these two methods respectively.
WaitGroup is a synchronization mechanism that can be used to wait for the end of a group of goroutines. The Add method is mainly used to increase the number of waiting goroutines, the Done method is used to reduce the number, and the Wait method waits for all goroutines to be executed. The following is a sample code:
package main import ( "fmt" "sync" ) func worker(id int, wg *sync.WaitGroup) { defer wg.Done() fmt.Printf("Worker %d is working ", id) } func main() { var wg sync.WaitGroup for i := 1; i <= 3; i++ { wg.Add(1) go worker(i, &wg) } wg.Wait() fmt.Println("All workers have finished") }
In the above code, we define a worker function to simulate a goroutine that needs to be executed, and then start 3 worker goroutines in the main function and wait through the Wait method They are executed.
Another common main function waiting strategy is to use channel. We can create a channel and let each goroutine send a signal to this channel when it ends. The main function can receive this signal to determine whether all goroutines have been executed. The following is a sample code:
package main import "fmt" func worker(id int, ch chan bool) { fmt.Printf("Worker %d is working ", id) ch <- true } func main() { numWorkers := 3 ch := make(chan bool, numWorkers) for i := 1; i <= numWorkers; i++ { go worker(i, ch) } for i := 1; i <= numWorkers; i++ { <-ch } fmt.Println("All workers have finished") }
In this example, we create a channel with a capacity of numWorkers and let each worker goroutine send a value to this channel at the end. The main function receives these values to determine whether all goroutines have been executed.
Summary
Through the above two specific code examples, we have learned about two common ways to implement the waiting strategy of the main function in the Go language: using the WaitGroup in the sync package and using the channel . In actual development, it is very important to choose an appropriate waiting strategy according to the specific situation, so as to ensure that the program can correctly wait for all goroutines to finish executing before exiting during concurrent execution.
The above is the detailed content of Decryption: Waiting strategy of main function in Go language. For more information, please follow other related articles on the PHP Chinese website!