Home >Backend Development >Golang >Go language concurrency control practice and application
"Practice and Application of Concurrency Control in Go Language"
In today's era of rapid development of information technology, concurrency control has become an indispensable factor in the field of software development. missing important themes. Among many programming languages, Go language has become one of the favorite languages used by developers because of its simplicity and efficiency. This article will delve into the concurrency control mechanism in Go language and combine it with specific code examples to give readers a deeper understanding of how to apply concurrency control in Go language.
Before introducing concurrency control in Go language, we must first understand the difference between concurrency and parallelism. Concurrency refers to the idea of program design that multiple parts of a program can be executed in parallel, but do not need to be executed at the same time. Parallelism refers to the execution of multiple parts of a program at the same time. In the Go language, goroutine is an important concept in achieving concurrency and can be understood as a lightweight thread. Below we will use code examples to show how to use goroutine to achieve concurrency control.
In the Go language, use the keyword go
to start a goroutine. Here is a simple example showing how to use goroutine to perform two tasks:
package main import ( "fmt" "time" ) func sayHello() { for i := 0; i < 5; i++ { fmt.Println("Hello") time.Sleep(1 * time.Second) } } func sayWorld() { for i := 0; i < 5; i++ { fmt.Println("World") time.Sleep(1 * time.Second) } } func main() { go sayHello() go sayWorld() time.Sleep(10 * time.Second) }
In the above code, we define two functions sayHello
and sayWorld
, output "Hello" and "World" respectively. In the main
function, two goroutines are started through the go
keyword to execute these two functions. Finally, time.Sleep
is used to wait long enough to ensure that the goroutine has enough time to execute.
In Go language, channel (channel) is a mechanism for communication between multiple goroutines, which can realize data between different goroutines. transfer. Here is an example showing how to use channels to control the execution order of goroutines:
package main import ( "fmt" ) func printMsg(msg string, ch chan int) { for i := 0; i < 5; i++ { fmt.Println(msg) } ch <- 1 } func main() { ch1 := make(chan int) ch2 := make(chan int) go printMsg("Hello", ch1) go printMsg("World", ch2) <-ch1 <-ch2 }
In the above code, we create two channels ch1
and ch2
, And use channels to control the execution order of goroutines. In the printMsg
function, we pass in a channel parameter ch
, and send a signal to the channel after the function is executed. In the main
function, use and <code> to wait for the execution of the goroutine to complete.
When multiple goroutines access shared resources at the same time, race conditions can easily occur. To prevent this from happening, you can use a mutex to protect shared resources. Here is an example showing how to use a mutex lock for concurrency control:
package main import ( "fmt" "sync" ) var count int var mutex sync.Mutex func increment() { mutex.Lock() defer mutex.Unlock() count++ } func main() { var wg sync.WaitGroup for i := 0; i < 1000; i++ { wg.Add(1) go func() { defer wg.Done() increment() }() } wg.Wait() fmt.Println("Count:", count) }
In the above code, we use a mutex lock mutex
to protect the global variable count
's access ensures that only one goroutine can operate on it at the same time. Use sync.WaitGroup
to wait for all goroutines to be executed, and finally output the result of count
.
This article introduces the practice and application of concurrency control in Go language through specific example codes. By using mechanisms such as goroutines, channels, and mutex locks, you can better control the order of concurrent execution and the security of accessing shared resources. I hope this article can help readers gain a deeper understanding of the application of concurrent programming in the Go language.
[Number of words: 997]
The above is the detailed content of Go language concurrency control practice and application. For more information, please follow other related articles on the PHP Chinese website!