Indeed this is what I do but I learned the hard way and often it comes up
as "safe" in the mailing list to not do so but while true from a memory
model perspective it is, I would say, wrong to do so.

mån 6 nov. 2017 kl 10:39 skrev Christoph Berger <
christoph.g.ber...@gmail.com>:

> Always call Add() before spawning the corresponding goroutine(s). And
> always call Add() in the same goroutine that also calls Wait(). This way
> you have no race condition between Add(), Done(), and Wait().
>
>
> On Saturday, November 4, 2017 at 10:46:21 AM UTC+1, Henrik Johansson wrote:
>
>> I find continuously adding and calling done on a waitgroup a bit odd. The
>> waiting goroutine can continue as soon as the count is zero so there is  a
>> race between adds and dones.
>>
>> On Sat, 4 Nov 2017, 10:01 , <28911...@gmail.com> wrote:
>>
> Thank you very much.Sorry I still have some question:what's the function
>>> of wg? what does the sentense "wg.Add(2) " mean?? I found that if I change
>>> 2 into 1,the result is differnet.
>>>
>>> 在 2017年11月3日星期五 UTC+8上午4:45:47,snmed写道:
>>>
>>>> Hi
>>>>
>>>> Here is the code:
>>>>
>>>> Version 1:
>>>> package main
>>>>
>>>> import (
>>>> "fmt"
>>>> "sync"
>>>> )
>>>>
>>>> var a string
>>>> var once sync.Once
>>>>
>>>> func setup() {
>>>> a = "hello,world\n"
>>>> }
>>>> func doprint() {
>>>> once.Do(setup)
>>>> fmt.Print(a)
>>>> }
>>>> func twoprint() <-chan struct{} {
>>>> var wg sync.WaitGroup
>>>> wg.Add(2)
>>>> ch := make(chan struct{})
>>>>
>>>> go func() {
>>>> doprint()
>>>> wg.Done()
>>>> }()
>>>> go func() {
>>>> doprint()
>>>> wg.Done()
>>>> }()
>>>>
>>>> go func() {
>>>> wg.Wait()
>>>> close(ch)
>>>> }()
>>>>
>>>> return ch
>>>> }
>>>>
>>>> func main() {
>>>> ch := twoprint()
>>>> <-ch
>>>> }
>>>>
>>>>
>>>> Version 2:
>>>> package main
>>>>
>>>> import (
>>>> "fmt"
>>>> "sync"
>>>> )
>>>>
>>>> var a string
>>>> var once sync.Once
>>>>
>>>> func setup() {
>>>> a = "hello,world\n"
>>>> }
>>>> func doprint() {
>>>> once.Do(setup)
>>>> fmt.Print(a)
>>>> }
>>>> func twoprint() {
>>>> var wg sync.WaitGroup
>>>> wg.Add(2)
>>>>
>>>> go func() {
>>>> doprint()
>>>> wg.Done()
>>>> }()
>>>> go func() {
>>>> doprint()
>>>> wg.Done()
>>>> }()
>>>>
>>>> wg.Wait()
>>>> }
>>>>
>>>> func main() {
>>>> twoprint()
>>>> }
>>>>
>>>>
>>>> Cheers snmed
>>>>
>>>>
>>>> Am Donnerstag, 2. November 2017 10:37:15 UTC+1 schrieb
>>>> 28911...@gmail.com:
>>>>>
>>>>> Sorry,I try my best to open the website but it can't work.Can you
>>>>> write it ??Thank you so much.
>>>>>
>>>>> 在 2017年10月30日星期一 UTC+8下午4:29:44,snmed写道:
>>>>>>
>>>>>> Hi
>>>>>>
>>>>>> There are several ways to solve it, here are two of them:
>>>>>>
>>>>>> https://play.golang.org/p/wJwkI7HQwv
>>>>>> https://play.golang.org/p/nasUcgBeG4
>>>>>>
>>>>>> I prefer the first one, because so I can decide if i want to wait for
>>>>>> the end of twoprint or not.
>>>>>>
>>>>>> Cheers
>>>>>>
>>>>>> Am Montag, 30. Oktober 2017 06:43:45 UTC+1 schrieb 28911...@gmail.com
>>>>>> :
>>>>>>>
>>>>>>> Yes, you're right.So how to solve it??
>>>>>>>
>>>>>>> 在 2017年10月30日星期一 UTC+8下午12:37:49,Dave Cheney写道:
>>>>>>>>
>>>>>>>> Hello. I’m guessing that your tried calling twoprint(), but you’ve
>>>>>>>> probably found that nothing is printed to the screen before your 
>>>>>>>> program
>>>>>>>> exits.
>>>>>>>>
>>>>>>>> Is that correct?
>>>>>>>
>>>>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "golang-nuts" group.
>>>
>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to golang-nuts...@googlegroups.com.
>>
>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to