Go language: Waitgourp use

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

When learning the go language, the WAITGOURP structure in the "Sync" package is used to achieve the completion of the control task.

Since Waitgroup is a structure, all needs to pass through sync. waitgroup{} to create

Very similar to the Countdownlatch class in the latch countdownlatch when the Shoper/java concurrent library is inverted.

ADD (), Done (), Wait (). where done () is the alias of Add (-1). Simply put, using Add () adds a count, done () minus a count, the count is not 0, and the wait () is blocked from running.

Package Mainimport ("FMT" "Runtime" "Sync") func main () {runtime. Gomaxprocs (runtime. NUMCPU ()) Waitgourp: = Sync. Waitgroup{}waitgourp.add (Ten) for I: = 0; I < 10; i++ {Go Go (&waitgourp, i)}waitgourp.wait ()}func Go (WG *sync. Waitgroup, index int) {sum: = 0for I: = 0; i < 100000000; i++ {sum + = i}fmt. PRINTLN (index, SUM) WG. Done ()}

PS: Because the previous use of Java, the method's parameter pass is the value of the pass, and in this go is the difference between the value of the pass and the reference passed.

Because the structure is a value type, the reference (address) of the struct should be passed as a method parameter.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.