In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to use WaitGroup in Go. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Under normal circumstances, the termination process of the newly activated goroutine (cooperative program) is uncontrollable, and the only way to guarantee the termination of the goroutine (cooperative program) is the termination of the main goroutine (cooperative program). In other words, we don't know which goroutine will end.
But in many cases, we just need to know whether the goroutine is complete. This needs to be done with the help of the WaitGroup of the sync package.
WatiGroup is a struct type in the sync package that is used to collect goroutine (co-procedures) that need to wait for execution to complete. Here is the definition of it:
Type WaitGroup struct {/ / Has unexported fields.} A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. Then each of the goroutines runs and calls Done when finished. At the same time, Wait can be used to block until all goroutines have finished. A WaitGroup must not be copied after first use.func (wg * WaitGroup) Add (delta int) func (wg * WaitGroup) Done () func (wg * WaitGroup) Wait ()
It has three ways:
Add (): call Add () to set or add the number of goroutine to be completed before activating the goroutine that you want to be completed.
For example, Add (2) or two calls to Add (1) will set the value of the waiting counter to 2, indicating that you want to wait for 2 goroutine (co-programs) to complete.
Done (): before the goroutine (cooperative program) that needs to wait is actually completed, this method should be called to indicate that the goroutine (cooperative program) is completed artificially. This method will subtract 1 from the waiting counter.
Wait (): Wait () blocks the current goroutine until the counter is reduced to 0.
In other words, Add () is used to increase the number of goroutine to wait for, Done () is used to indicate that the goroutine has been completed, the counter is reduced once, and Wait () is used to wait for all the goroutine (co-programs) to be completed.
The following is an example that is easy to understand.
Package mainimport ("fmt"sync"time") func process (I int, wg * sync.WaitGroup) {fmt.Println ("started Goroutine", I) time.Sleep (2 * time.Second) fmt.Printf ("Goroutine% d ended\ n", I) wg.Done ()} func main () {no: = 3 var wg sync.WaitGroup for I: = 0; I < no ITunes + {wg.Add (1) go process (I, & wg)} wg.Wait () fmt.Println ("All go routines finished executing")}
Three goroutine are activated above, and each time before activating goroutine, the Add () method is called to increase the goroutine count that needs to be waited. Each goroutine runs the process () function, which needs to call the Done () method to indicate the end of the goroutine when execution is complete. After activating three goroutine, main goroutine executes to Wait (). Since each activated goroutine runs process () that requires 2 seconds of sleep, main goroutine will block here in Wait () for a period of time (about 2 seconds). When all goroutine is complete, the waiting counter reduced to 0MagneWit () will no longer block, so main goroutine can execute the following Println ().
It is also important to note that the pointer type * sync.WaitGroup is used as a parameter in process (). You cannot use a value type sync.WaitGroup as a parameter here, because this means that each goroutine makes a copy of wg, and each goroutine uses its own wg. This is obviously unreasonable. The three goroutine should share a wg to know that all three goroutine have been completed. In fact, if you use a parameter of a value type, main goroutine will permanently block, resulting in a deadlock.
These are all the contents of the article "how to use WaitGroup in Go". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.