Skip to content

question about the channel #7879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gopherbot opened this issue Apr 27, 2014 · 2 comments
Closed

question about the channel #7879

gopherbot opened this issue Apr 27, 2014 · 2 comments

Comments

@gopherbot
Copy link
Contributor

by jikai507:

package main

import (
    "fmt"
)

func foo(ch chan string) {
    defer func() {
        ch <- "finish"
    }()

    // do some process
    // ...
}

func main() {
    ch := make(chan string)
    for i := 0; i <= 100; i++ {
        ch <- "start"
        go foo(ch)
    }

    var (
        msg        string
        allroutine int
        finished   int
    )
    // In this loop, I record two flags of goroutines.
    // If a routine start, a "start" message will emit 
    // to the channel.
    // If a routine finish, a "finish" message will emit
    // to the channel.
    // The variable allroutine always bigger than variable
    // finished because routine first start and finish
    // later.
    // If finished equal to the allroutine, it means
    // that all routines finished already. 
    for {
        msg = <-ch
        if msg == "start" {
            allroutine++
        }
        if msg == "finish" {
            finished++
        }
        fmt.Println("allroutine:", allroutine, "finished:", finished)
        if finished == allroutine {
            break
        }
    }
}


What does 'go version' print?
go v1.2.1

What happened?
deadlock...

What should have happened instead?
It will finished with the output "allroutine: 100 finished: 100".

Please provide any additional information below.
@cznic
Copy link
Contributor

cznic commented Apr 27, 2014

Comment 1:

The issue is in the code, not in Go: http://play.golang.org/p/nEovFS2P9N
#Invalid

@bradfitz
Copy link
Contributor

Comment 2:

Status changed to Invalid.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants