FAQ
Hi All,

I'm trying a basic multithreaded program in go. I fire a parallel goroutine
and wait for it to finish.

package main

import (
"fmt"
"sync"
"time"
)

func main() {
messages := make(chan int)

var wg sync.WaitGroup

wg.Add(1)

go func() {
defer wg.Done()
time.Sleep(time.Second * 3)
messages <- 1
fmt.Println("Stage 1 Posted")
}()

fmt.Println("Stage 1 Ends")

wg.Wait()

close(messages)
}

However, I see an error which i believe is not correct.

$ ./go-examples

Stage 1 Ends

fatal error: all goroutines are asleep - deadlock!


goroutine 1 [semacquire]:

sync.runtime_Semacquire(0x8201ca2bc)

/usr/local/go/src/runtime/sema.go:47 +0x26

sync.(*WaitGroup).Wait(0x8201ca2b0)

/usr/local/go/src/sync/waitgroup.go:127 +0xb4

main.main()

/Users/satyam/ws/src/github.com/satyamsi/go-examples/main.go:25 +0x163


goroutine 5 [chan send]:

main.main.func1(0x8201ca2b0, 0x8201d80c0)

/Users/satyam/ws/src/github.com/satyamsi/go-examples/main.go:19 +0x86

created by main.main

/Users/satyam/ws/src/github.com/satyamsi/go-examples/main.go:21 +0x9a


Is there a requirement that the programmer has to make sure that there is
always an idle thread ? Or is this a bug ?


Satyam

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jan Mercl at Mar 17, 2016 at 3:13 pm

    On Thu, Mar 17, 2016 at 4:08 PM satyam@gmail.com wrote:

    Or is this a bug ?
    It's a bug. Sending to 'messages', an unbuffered channel, cannot proceed as
    there's nothing reading from it. So the defer wg.Done is never executed, so
    wg.Wait cannot proceed, so deadlock.

    --

    -j

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 17, '16 at 3:08p
activeMar 17, '16 at 3:13p
posts2
users2
websitegolang.org

2 users in discussion

Jan Mercl: 1 post Satyam: 1 post

People

Translate

site design / logo © 2022 Grokbase