FAQ
Hello,

I have some code which looks like this:

package main

import (

"fmt"

)


func main() {

a, b := make(chan string), make(chan string)

go func() { a <- "a" }()

go func() { b <- "b" }()


for {

select {

case s := <-a:

fmt.Println("got", s)

case s := <-b:

fmt.Println("got", s)

}

}

}
The code is modified based on the code in this
<http://talks.golang.org/2013/advconc.slide#29> presenation. When I compile
the code above and run the program I get this error message:

got a
got b

fatal error: all goroutines are asleep - deadlock!


goroutine 1 [select]:

main.main()

C:/Dev/Misc/Go/goprojects/src/Scratch/main.go:13 +0x564
The reason for creating this thread is now not to lament about the fatal
error. What I'm interested in is to understand how Go detects that all
goroutines are asleep. Is this simply a timeout that goes off?

Thanks, Haddock

--
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 May 28, 2015 at 11:36 am

    On Thu, May 28, 2015 at 12:55 PM Haddock wrote:

    Is this simply a timeout that goes off?
    I haven't checked how the runtime detects this, but:

    if len(listOfRunningGoroutines) == 0 && len(listOfGoroutinesReadyToRun) ==
    0 {
             // We have a deadlock as all goroutines must be in the
             // listOfBlockedGoroutines because the number of goroutines
             // during the lifetime of a program is always > 0.
    }

    -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.
  • Haddock at May 28, 2015 at 11:58 am
    Thanks Jan. If I get it right you are saying that what Go detects in this
    case is a "global deadlock", e.g. no activity at all. Right?

    Am Donnerstag, 28. Mai 2015 13:36:25 UTC+2 schrieb Jan Mercl:
    On Thu, May 28, 2015 at 12:55 PM Haddock <ffm...@web.de <javascript:>>
    wrote:
    Is this simply a timeout that goes off?
    I haven't checked how the runtime detects this, but:

    if len(listOfRunningGoroutines) == 0 && len(listOfGoroutinesReadyToRun) ==
    0 {
    // We have a deadlock as all goroutines must be in the
    // listOfBlockedGoroutines because the number of goroutines
    // during the lifetime of a program is always > 0.
    }

    -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.
  • Dave Cheney at May 28, 2015 at 1:16 pm
    This is part of the scheduler. As each goroutine blocks or yields, the thread servicing that goroutine goes back to the scheduler to find more work. If there is no work, the thread sleeps, if there is no work and all the other threads in the program are also sleeping then by definition nothing will wake them up, so the program is deadlocked.

    --
    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
postedMay 28, '15 at 10:55a
activeMay 28, '15 at 1:16p
posts4
users3
websitegolang.org

3 users in discussion

Haddock: 2 posts Dave Cheney: 1 post Jan Mercl: 1 post

People

Translate

site design / logo © 2022 Grokbase