The way to guarantee that you wait until goroutines are finished is by 
using sync.WaitGroup.

See 
https://www.programming-books.io/essential/go/139-wait-for-goroutines-to-finish 
for example and longer description.

Alternatively, use range over a channel and arrange the code so that you 
close the channel once when all sends are done. 
See https://www.programming-books.io/essential/go/144-closing-channels

On Saturday, March 24, 2018 at 3:41:19 AM UTC-7, T L wrote:
>
> In the following example, there are 99 goroutines queuing and blocking on 
> sending a value to c.
> When the only buffered value is received, it looks there is a time 
> interval until the buffer is filled.
> Shouldn't it be that the receive from the only buffer and fill next 
> queuing value to the only buffer in one atomic operation?
>
>
> package main
>
> import "time"
>
> func main() {
>     c := make(chan int, 1)
>     for i := 0; i < 100; i++ {
>         go func() {
>             c <- 1
>         }()
>     }
>     
>     time.Sleep(time.Second)
>     
>     n := 0
>     for len(c) == cap(c) {
>         <-c
>         println(n, len(c)) // here, len(c) is always 0
>         n++
>         // time.Sleep(time.Second/1000) // if this line is not commented 
> off, there will be 100 lines output.
>     }
> }
>

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