I think you can still deadlock, but I'm not sure if the `default:` case is
considered for random selection of a select like other branches are. It
would just be less deterministic.

On Thu, Sep 1, 2016 at 10:38 AM Darren Hoo <darren....@gmail.com> wrote:

> Got it, using buffered channel works for me
>
> ints := make(chan int, 1)
> done := make(chan bool, 1)
>
> go func() {
> for i := 0; i < 100; i++ {
> ints <- i
> }
> close(ints)
> }()
>
> f := func() {
> j := 0
> for {
> i, ok := <-ints
> if !ok {
> done <- true
> return
> }
> j++
>
> fmt.Println(i)
>
> //10 ints for one batch
> if j == 10 {
> return
> }
> }
> }
>
> exit:
> for {
> f()
>
> select {
> case <-done:
> fmt.Println("Exiting")
> break exit
> default:
> //do nothing just next f()
> }
> }
>
>
>
> On Friday, September 2, 2016 at 1:30:10 AM UTC+8, freeformz wrote:
>
>> If you execute it you are told on which lines the deadlock happens...
>>
>> fatal error: all goroutines are asleep - deadlock!
>>
>> goroutine 1 [chan send]:
>> main.main.func2()
>>         /Users/emuller/go/src/github.com/freeformz/tt/main.go:25 +0xce
>> main.main()
>>         /Users/emuller/go/src/github.com/freeformz/tt/main.go:37 +0x118
>> exit status 2
>>
>> main.go:25 is the line that says `done <- true`.
>>
>> The `done` channel isn't buffered and the first loop through will select
>> that path, but there is not another goroutine selecting from done. so f()
>> never returns. Deadlock.
>>
>> On Thu, Sep 1, 2016 at 10:04 AM Darren Hoo <darre...@gmail.com> wrote:
>>
> var wg sync.WaitGroup
>>>
>>> ints := make(chan int, 1)
>>> done := make(chan bool)
>>>
>>> go func() {
>>> for i := 0; i < 3; i++ {
>>> ints <- i
>>> }
>>> close(ints)
>>> }()
>>>
>>> f := func() {
>>> wg.Add(1)
>>> defer wg.Done()
>>>
>>> for {
>>> i, ok := <-ints
>>> if !ok {
>>> done <- true
>>> return
>>> }
>>> }
>>> }
>>>
>>> exit:
>>> for {
>>> select {
>>> case <-done:
>>> break exit
>>> default:
>>> f()
>>> }
>>> }
>>> wg.Wait()
>>>
>>>
>>>
>>> --
>>> 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