Go language, as a concurrent programming language, provides rich mechanisms to support collaboration between multiple goroutines. In concurrent programming, synchronization and mutual exclusion are two important concepts. This article will explore synchronization and mutual exclusion in the Go language, and illustrate it with specific code examples.
1. Synchronization
In concurrent programming, synchronization refers to coordinating the execution order of multiple goroutines to ensure that they execute in a certain order and avoid problems such as race conditions. In the Go language, commonly used synchronization mechanisms include Channel, WaitGroup, etc.
Channel is an important mechanism in the Go language used to transfer data and synchronize between goroutines. Synchronization between goroutines can be achieved through Channel to ensure the sequential execution of certain operations.
The following is a sample code for synchronization using Channel:
package main import ( "fmt" ) func main() { ch := make(chan int) done := make(chan bool) go func() { fmt.Println("Goroutine 1") ch <- 1 }() go func() { fmt.Println("Goroutine 2") <-ch done <- true }() <-done fmt.Println("Main goroutine") }
In the above code, we create an unbuffered Channel ch and create a Channel for notification completion done. The two goroutines print "Goroutine 1" and "Goroutine 2" respectively, and then synchronize through Channel ch. Finally, the main goroutine waits for the message from the done channel and prints "Main goroutine" to indicate that the execution is completed.
WaitGroup is a synchronization mechanism provided in the sync package, which can wait for a group of goroutines to complete before continuing execution.
The following is a sample code using WaitGroup for synchronization:
package main import ( "fmt" "sync" ) func main() { var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() fmt.Println("Goroutine 1") }() go func() { defer wg.Done() fmt.Println("Goroutine 2") }() wg.Wait() fmt.Println("Main goroutine") }
In the above code, we created a WaitGroup wg and added 2 goroutines through the Add method. After each goroutine completes the task, it calls the Done method to notify the WaitGroup. Finally, the main goroutine calls the Wait method to wait for all goroutines to complete execution.
2. Mutual exclusion
When multiple goroutines access shared resources at the same time, race conditions may occur, leading to data conflicts and incorrect results. Mutual exclusion refers to locking shared resources to ensure that only one goroutine can access shared resources at the same time. In Go language, you can use Mutex in the sync package to implement mutual exclusion.
The following is a sample code for using Mutex for mutual exclusion:
package main import ( "fmt" "sync" ) var count int var mu sync.Mutex func increment() { mu.Lock() count++ mu.Unlock() } func getCount() int { mu.Lock() defer mu.Unlock() return count } func main() { for i := 0; i < 10; i++ { go increment() } fmt.Println("Final count:", getCount()) }
In the above code, we define a global variable count and a Mutex mu. The increment function uses Mutex to ensure concurrency safety when incrementing count. The main goroutine creates 10 goroutines to perform increment operations concurrently, and finally obtains the final count value through the getCount function and prints it out.
In summary, this article discusses synchronization and mutual exclusion in the Go language and provides specific code examples for illustration. Through appropriate synchronization and mutual exclusion mechanisms, collaboration between goroutines can be effectively managed to ensure program correctness and performance. In actual concurrent programming, it is necessary to choose appropriate synchronization and mutual exclusion methods according to specific scenarios to improve the reliability and efficiency of the program.
The above is the detailed content of Discuss synchronization and mutual exclusion in Go language. For more information, please follow other related articles on the PHP Chinese website!