If you were to do that, yes.  I guess it come to: How do you define “done” in 
your program?  Waitgroup defines it as when all the goroutines which it counted 
are closed.

 

John

    John Souvestre - New Orleans LA

 

From: golang-nuts@googlegroups.com [mailto:golang-nuts@googlegroups.com] On 
Behalf Of Henrik Johansson
Sent: 2017 November 04, Sat 04:46
To: 2891132l...@gmail.com
Cc: golang-nuts
Subject: Re: [go-nuts] Re: concurrency programing about go

 

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 , <2891132l...@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+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.

-- 
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