Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Go concurrency

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to understand Go concurrency. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Introduction to the use of WaitGroup in Go concurrency.

For Go concurrent programming, the master process needs to know when the other programs are finished. Usually our approach is to use the channel approach to control, which is good, you can see my other post (Go concurrent channel: https://mp.weixin.qq.com/s/PIb-gGBootc6581pHhi5ew). But for some simple co-program control, channel seems to be overqualified, and WaitGroup can be used.

I. definition of WaitGroup

WaitGroup is the content of the sync package in Go pkg: https://golang.org/pkg/sync/, a WaitGroup waits for a series of goroutine until they are all running. The main goroutine sets the number of goroutine to wait by calling the Add method, while each running goroutine calls the Done method when they are finished running. At the same time, calling the Wait method can block until all the goroutine is running.

1. An example of a master process that does nothing to other programs:

As can be seen from the output above, the main program exits itself without waiting for the end of the other programs, so no printing of other programs can be seen.

two。 An example of a master program waiting for other processes to end through a timeout mechanism:

A new timeout of 1s is added to the main program to wait for the end of the other programs. It can be seen by printing that the other programs have been run and finished.

3. Examples of using wg:

Through the printing information, we can see that the main cooperative program determines whether the counter has changed to 0 through wg.Wait () to determine whether the other cooperative programs have finished. After the main cooperative program detects that the counter of wg has changed to 0, we will know that the other cooperative programs have been finished.

II. Introduction of API in WaitGroup

1. (* WaitGroup) Add method

Func (wg*WaitGroup) Add (delta int)

Delta can be positive or negative, and delta can be negative before the counter corresponding to wg becomes 0, but remember not to let the wg counter become negative, because once it becomes negative, it will panic.

Add is usually used before the start of the collaboration process.

2. (* WaitGroup) Done method

Func (wg*WaitGroup) Done ()

Subtract 1 from the wg counter.

Done is usually used at the end of the execution of a collaborative program.

3. (* WaitGroup) Wait method

Func (wg*WaitGroup) Wait ()

Block until the value of the WaitGroup counter is 0.

It is usually used in the main collaboration process to wait for the end of other collaborations.

The above is the editor for you to share how to understand Go concurrency, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report