Home >Backend Development >Golang >How Can I Control the Order of Execution of Goroutines in Go?
Goroutines Order of Execution Unpredictability
In the provided code snippet, the order of execution for the two goroutines is non-deterministic. The output shows that the second goroutine executes first, even though it was started after the first. This behavior is due to the concurrent nature of goroutines, which means that they execute independently from each other.
Synchronization Mechanisms in Goroutines
To control the order of goroutine execution, you can use synchronization mechanisms provided by Go, such as:
Channels: You can use channels to synchronize the execution of goroutines. A channel is a communication channel that allows goroutines to send and receive data. In the modified example below, the channel is used to block the main goroutine until the first goroutine completes its execution before the second goroutine is started.
func main() { c := make(chan int) go sum([]int{1, 2, 3}, c) // Use the channel to block until it receives a send x := <-c fmt.Println(x) // Then execute the next routine go sum([]int{4, 5, 6}, c) x = <-c fmt.Println(x) }
Wait Groups: A wait group is another synchronization mechanism that allows you to wait for multiple goroutines to complete execution before proceeding. In the following example, a wait group is used to ensure that all goroutines are finished before the main goroutine exits.
func sum(a []int, c chan int, wg *sync.WaitGroup) { defer wg.Done() fmt.Println("summing: ", a) total := 0 for _, v := range a { total += v } // Send total to c c <- total } func main() { c := make(chan int) wg := new(sync.WaitGroup) // Concurrently call the concurrent calls to sum, allowing execution to continue to the range of the channel go func() { // Increment the wait group, and pass it to the sum func to decrement it when it is complete wg.Add(1) go sum([]int{1, 2, 3}, c, wg) // Wait for the above call to sum to complete wg.Wait() // And repeat... wg.Add(1) go sum([]int{4, 5, 6}, c, wg) wg.Wait() // All calls are complete, close the channel to allow the program to exit cleanly close(c) }() // Range of the channel for theSum := range c { x := theSum fmt.Println(x) } }
By using these synchronization mechanisms, you can control the order of goroutine execution and ensure that the desired sequence of operations is maintained.
The above is the detailed content of How Can I Control the Order of Execution of Goroutines in Go?. For more information, please follow other related articles on the PHP Chinese website!