Re: [go-nuts] Re: concurrency programing about go

2017-11-06 Thread Henrik Johansson
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.


Re: [go-nuts] Re: concurrency programing about go

2017-11-06 Thread Karan Chaudhary
Just a note that waitGroup should be passed as reference to 
functions/methods rather than as value.  I have made mistakes where the 
program hung as Done was called on copy of waitgroup rather than on the 
original wg to which tasks were added.

Here it is not a problem as waitgroup used within anon functions is taken 
from scope.

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


Re: [go-nuts] Re: concurrency programing about go

2017-11-06 Thread Christoph Berger
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.


RE: [go-nuts] Re: concurrency programing about go

2017-11-04 Thread John Souvestre
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.


Re: [go-nuts] Re: concurrency programing about go

2017-11-04 Thread Henrik Johansson
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.