FAQ
This is the problem I'm trying to solve:

My program fires of a bunch of goroutines. Each of these goroutines
communicates back with my main program using a boolean channel. If any of
these channels return false, my main function will return right away. If *
all* the channels are true, however, I want to return something different.
I also have a timeout of 1 second. My code looks like:

import "time"
import "ft"

func someFunction() bool {
     chan1, chan2, chan3 := new(chan bool)
     go func() {
         if xyz { chan1 <- false }
         chan1 <- true
     }()

     go func() {
         if yyz { chan2 <- false }
         chan2 <- true
     }()

     go func() {
         if zzz { chan3 <- false }
         chan3 <- true
     }()

     timeout := time.After(1 * time.Second)
     for {
         select {
             case val1 := <- chan1:
                 if !val1 {return false}
             case val2 := <- chan2:
                 if !val2 {return false}
             case val3 := <- chan3:
                 if !val3 {return false}
             case <- timeout:
                 fmt.Println("timeout!")
                 return false
         }
     }
     return true
}

The problem that I'm running into is that the 'timeout' code always gets
executed. The code never makes it to the return true.

Any help would be appreciated.


--
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/groups/opt_out.

Search Discussions

  • Nigel Tao at Jul 19, 2013 at 1:08 am
    chan1, chan2, chan3 := new(chan bool)
    This doesn't even compile. Can you show real code?

    --
    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/groups/opt_out.
  • Liigo Zhuang at Jul 20, 2013 at 11:35 pm

    On Jul 19, 2013 9:08 AM, "Nigel Tao" wrote:
    chan1, chan2, chan3 := new(chan bool)
    This doesn't even compile. Can you show real code?
    It's enough to find the probIem, just like tad did.
    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Tad Glines at Jul 19, 2013 at 1:13 am
    The "return true" statement is never reached because "for {}" is an
    infinite loop.
    Add a count, and increment the count in each of the chan cases, then check
    the count at the end of the select. If the count == the number of chans,
    then return true.

    Here's an example: http://play.golang.org/p/Enw7UFCIpB

    On Thu, Jul 18, 2013 at 7:41 AM, M R wrote:

    This is the problem I'm trying to solve:

    My program fires of a bunch of goroutines. Each of these goroutines
    communicates back with my main program using a boolean channel. If any of
    these channels return false, my main function will return right away. If *
    all* the channels are true, however, I want to return something
    different. I also have a timeout of 1 second. My code looks like:

    import "time"
    import "ft"

    func someFunction() bool {
    chan1, chan2, chan3 := new(chan bool)
    go func() {
    if xyz { chan1 <- false }
    chan1 <- true
    }()

    go func() {
    if yyz { chan2 <- false }
    chan2 <- true
    }()

    go func() {
    if zzz { chan3 <- false }
    chan3 <- true
    }()

    timeout := time.After(1 * time.Second)
    for {
    select {
    case val1 := <- chan1:
    if !val1 {return false}
    case val2 := <- chan2:
    if !val2 {return false}
    case val3 := <- chan3:
    if !val3 {return false}
    case <- timeout:
    fmt.Println("timeout!")
    return false
    }
    }
    return true
    }

    The problem that I'm running into is that the 'timeout' code always gets
    executed. The code never makes it to the return true.

    Any help would be appreciated.


    --
    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/groups/opt_out.

    --
    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/groups/opt_out.
  • Peter Bourgon at Jul 23, 2013 at 9:45 am
    As written, and in Tad's version, the program also potentially leaks
    all 3 of the initial goroutines, if the timeout is hit. Assuming you
    meant those `chanN <- true`s to be in an 'else' block, you can
    (should) change chanN to have a buffer of size 1.


    On Thu, Jul 18, 2013 at 4:41 PM, M R wrote:
    This is the problem I'm trying to solve:

    My program fires of a bunch of goroutines. Each of these goroutines
    communicates back with my main program using a boolean channel. If any of
    these channels return false, my main function will return right away. If all
    the channels are true, however, I want to return something different. I also
    have a timeout of 1 second. My code looks like:

    import "time"
    import "ft"

    func someFunction() bool {
    chan1, chan2, chan3 := new(chan bool)
    go func() {
    if xyz { chan1 <- false }
    chan1 <- true
    }()

    go func() {
    if yyz { chan2 <- false }
    chan2 <- true
    }()

    go func() {
    if zzz { chan3 <- false }
    chan3 <- true
    }()

    timeout := time.After(1 * time.Second)
    for {
    select {
    case val1 := <- chan1:
    if !val1 {return false}
    case val2 := <- chan2:
    if !val2 {return false}
    case val3 := <- chan3:
    if !val3 {return false}
    case <- timeout:
    fmt.Println("timeout!")
    return false
    }
    }
    return true
    }

    The problem that I'm running into is that the 'timeout' code always gets
    executed. The code never makes it to the return true.

    Any help would be appreciated.


    --
    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/groups/opt_out.
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 19, '13 at 1:02a
activeJul 23, '13 at 9:45a
posts5
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase