goroutine is a lightweight thread implementation in the Go language and is managed by the Go runtime. Go programs intelligently assign tasks in goroutine to each CPU wisely.
01 Introduction
One of the advantages of the Golang language is that it is naturally supported by concurrency. In Golang language development, the concurrency control methods we usually use are Channel, WaitGroup and Context. In this article, we will mainly introduce how to use these three methods of concurrency control in Golang language? The detailed introduction of each of them has been introduced in previous articles. Interested readers can read them as needed.
02Channel
In the Golang language, Channel can not only be used for communication between coroutines, but also use Channel to control subcoroutines. It is also simple to use Channel to implement concurrent control. For example, in the following example, we start two coroutines in the Golang application, namely the main coroutine and the subcoroutine. The main coroutine needs to wait for the child coroutine to run before exiting the program.
Sample code:
func main () { done := make(chan struct{}) go func() { ("goroutine run over") done <- struct{}{} }() <- done ("main goroutine run over") }
Read the above code. After the child goroutine is run, we notify the main goroutine to exit the program through the Channel. In fact, we can also handle it in reverse. The main goroutine notifies the child goroutine to exit the program, the main goroutine sends data to the channel, and the child goroutine is waiting to receive the data in the channel.
If in a Golang application, you need to make the main goroutine wait for multiple goroutines to run and then exit the program, how should we implement it? Yes, it can also be implemented using Channel, but there is a more elegant way to implement it, that is WaitGroup. As the name suggests, WaitGroup is to wait for a group of goroutines to run and end.
Sample code:
func main () { wg := {} (10) for i := 0; i < 10; i++ { go func(id int) { (id, "Run ends") () }(i) } () ("main goroutine run over") }
Read the above code, we start 10 sub goroutines. The main goroutine needs to wait for 10 sub goroutines to run before exiting the program. We use WaitGroup, which has three methods, namely Add, Done and Wait. In fact, WaitGroup maintains a counter. These three methods work around this counter. Add is used to set the counter value, Done is used to deduct the counter value, and Wait is blocked until the counter value is 0. The source code interpretation of WaitGroup has been introduced in previous articles. Due to space limitations, I will not repeat it here.
04Context
Channel and WaitGroup are usually used in concurrent control of applications with parent-child goroutine levels. If in Golang applications, the child coroutine continues to derive coroutines, how should we control it? We can use Context to implement concurrent control for such multi-level goroutine applications.
Sample code:
func main() { ctx, cancel := (()) go firstCtx(ctx) (5 * ) ("stop all sub goroutine") cancel() (5 * ) } func firstCtx(ctx ) { go secondCtx(ctx) for { select { case <-(): ("first done") return default: ("first running") (2 * ) } } } func secondCtx(ctx ) { for { select { case <-(): ("second done") return default: ("second running") (2 * ) } } }
Read the above code, start the child coroutine secondCtx in the child coroutine firstCtx, create a context in the main goroutine, and pass the context to all child coroutines, and then the main goroutine stops all child coroutines by calling cancer.
05 Summary
In this article, we introduce which way to control concurrent goroutines is suitable for different scenarios. Among them, channel is suitable for controlling a small number of concurrent goroutines, WaitGroup is suitable for controlling a set of concurrent goroutines, and context is suitable for controlling multi-level concurrent goroutines.
This is the article about the method of Golang language control concurrency Goroutine. For more information about Golang concurrency control Goroutine, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!