In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about an example analysis of WaitGroup traps in Golang. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
Golang's WaitGroup Trap
sync.WaitGroup is a fairly common data structure in concurrency environments, used to wait for the end of all coroutines, written as examples, and without delving into its use. A few days ago, I thought about whether I could execute the Add() function in the coroutine. The answer is no. Here is the introduction.
The trap is in the order in which the three WaitGroup functions are called. Let's review the functions of the following three functions:
Add(delta int): Increments the counter by delta, e.g. 1 for starting 1 coroutine.
Done(): Executes before coroutine exits, decrements counter by 1.
Wait(): Block wait counter is 0.
to test
The following program creates a co-routine father, and then the father co-routine creates 10 sub-coroutines, and the main function waits for all co-routines to end and exits. See if there is any problem with the following code?
package mainimport ( "fmt" "sync")func father(wg *sync.WaitGroup) { wg.Add(1) defer wg.Done() fmt.Printf("father\n") for i := 0; i < 10; i++ { go child(wg, i) }}func child(wg *sync.WaitGroup, id int) { wg.Add(1) defer wg.Done() fmt.Printf("child [%d]\n", id)}func main() { var wg sync.WaitGroup go father(&wg) wg.Wait() log.Printf("main: father and all chindren exit")}
Did you notice anything wrong? If you do not see the following results: main function starts to end before the end of the subroutine.
fathermain: father and all childrenexitchild [9]child [0]child [4]child [7]child [8] trap analysis
The reason for the above problem is that the Add() function is executed within the coroutine after the coroutine is created, and the Wait () function may already be executing at this time, even before all Add() functions are executed, Wait() immediately satisfies the WaitGroup counter is 0, Wait ends, and the main program exits, resulting in all subroutines not completely exiting, and the main function ends.
the right thing to do
Note that calls with a positive delta that occurs when the counter is zero must happen before a Wait.
How do I make sure that the Add function is executed before the Wait function? In the coroutine case, we cannot predict whether the code in the coroutine will execute earlier than the Wait function, but we can execute the Add function before assigning the coroutine, and then execute the Wait function to ensure that.
Below is the modified program, along with the output.
package mainimport ( "fmt" "sync")func father(wg *sync.WaitGroup) { defer wg.Done() fmt.Printf("father\n") for i := 0; i < 10; i++ { wg.Add(1) go child(wg, i) }}func child(wg *sync.WaitGroup, id int) { defer wg.Done() fmt.Printf("child [%d]\n", id)}func main() { var wg sync.WaitGroup wg.Add(1) go father(&wg) wg.Wait() fmt.Println("main: father and all children index exit")}fatherchild [9]child [7]child [8]child [1]child [4]child [5]child [2]child [6]child [0]child [3]main: father and all children index exit Thank you for reading! About "Golang WaitGroup trap example analysis" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
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.