It is fairly trivial to create a Spawner structure that wraps the wait group 
and the counter (max spawns) to do what you want. As others pointed out, 
waitempty can be racy in the general sense, and prone to incorrect usage. (I 
did not do a full analysis but I’ll take your word for it that it is not racy 
in this case)

> On Feb 22, 2019, at 6:59 AM, Serhat Sevki Dincer <jfcga...@gmail.com> wrote:
> 
> Yes workers could regulate number of spawns with an atomic counter themselves 
> but how would the "master caller" sleep until all those workers are done? It 
> needs to sleep until the counter is zero, Like a WaitGroup. 
> 
> 
> 22 Şub 2019 Cum 15:53 tarihinde Robert Engels <reng...@ix.netcom.com> şunu 
> yazdı:
>> But you can replace waitempty() and related  with a simple atomic counter 
>> variable and avoid channels for this. Far simpler. 
>> 
>>> On Feb 22, 2019, at 6:38 AM, Serhat Sevki Dincer <jfcga...@gmail.com> wrote:
>>> 
>>> A little imagination would help us all greatly. Select part would obviously 
>>> be in a for loop. Like this:
>>> 
>>> func worker() {
>>>   // do work,  prepare whatever 
>>> 
>>> for some_condition{
>>>   // do stuff
>>> 
>>>   Select {
>>>   ch <- true:
>>>     go worker() // try to handover some jobs
>>>   default:
>>>     // max goroutine limit
>>>     // do them yourself
>>>   }
>>> 
>>>   // do stuff, increment counter
>>> } 
>>> 
>>>   // do remaining jobs
>>> 
>>>   // make way
>>>   <- ch
>>>   return
>>> }
>>> 
>>> 22 Şub 2019 Cum 15:30 tarihinde Robert Engels <reng...@ix.netcom.com> şunu 
>>> yazdı:
>>>> I’m pretty sure this code is incorrect and there will only be a single 
>>>> routine doing any “real work”, although the comment //do remaining jobs is 
>>>> unclear because there is no code. 
>>>> 
>>>> > On Feb 22, 2019, at 12:42 AM, Serhat Şevki Dinçer <jfcga...@gmail.com> 
>>>> > wrote:
>>>> > 
>>>> > Another use case is wait groups with Max number of goroutines allowed:
>>>> > 
>>>> > ch:= make(chan bool, 10)
>>>> > 
>>>> > func waiter() {
>>>> >  ch <-true
>>>> >  go worker()
>>>> > 
>>>> >  // when empty, all jobs are finished
>>>> >  waitempty(ch)
>>>> > }
>>>> > 
>>>> > func worker() {
>>>> >  // do work
>>>> > 
>>>> >  Select {
>>>> >  ch <- true:
>>>> >    go worker() // try to handover some jobs
>>>> >  default:
>>>> >    // max goroutine limit
>>>> >    // do them yourself
>>>> >  } 
>>>> > 
>>>> >  // do remaining jobs
>>>> > 
>>>> >  // make way
>>>> >  <- ch
>>>> >  return
>>>> > }
>>>> > 
>>>> > -- 
>>>> > 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