FAQ
Please read this article first:

http://stackoverflow.com/questions/16105325/how-to-check-a-channel-is-closed-or-not-without-reading-it


"So I think the best way to solve this is worker() close channel when
exits, if the controller finds a channel closed, it will jump over it and
do nothing. But I can't find how to check a channel is already closed or
not in this situation. If I try to read the channel in controller, the
controller might be blocked. So I'm very confused for now."

PS: Recovering the raised panic is what I have tried, but it will close
goroutine which raised panic. In this case it will be controller so it's no
use.

Still, I think it's useful for go team to implement this function in next
version of Go.

--
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dave Cheney at Apr 19, 2013 at 1:43 pm
    I read the linked article, but still don't really understand the question. I think what you are looking for is the two argument version of channel receive, v, ok := <- c, combined with a select statement to make them non blocking.


    On 19/04/2013, at 11:38 PM, Reck Hou wrote:

    Please read this article first:

    http://stackoverflow.com/questions/16105325/how-to-check-a-channel-is-closed-or-not-without-reading-it


    "So I think the best way to solve this is worker() close channel when exits, if the controller finds a channel closed, it will jump over it and do nothing. But I can't find how to check a channel is already closed or not in this situation. If I try to read the channel in controller, the controller might be blocked. So I'm very confused for now."

    PS: Recovering the raised panic is what I have tried, but it will close goroutine which raised panic. In this case it will be controller so it's no use.

    Still, I think it's useful for go team to implement this function in next version of Go.

    --
    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 [email protected].
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at Apr 19, 2013 at 1:44 pm

    On Fri, Apr 19, 2013 at 9:38 PM, Reck Hou wrote:

    Please read this article first:


    http://stackoverflow.com/questions/16105325/how-to-check-a-channel-is-closed-or-not-without-reading-it

    "So I think the best way to solve this is worker() close channel when
    exits, if the controller finds a channel closed, it will jump over it and
    do nothing. But I can't find how to check a channel is already closed or
    not in this situation. If I try to read the channel in controller, the
    controller might be blocked. So I'm very confused for now."

    PS: Recovering the raised panic is what I have tried, but it will close
    goroutine which raised panic. In this case it will be controller so it's no
    use.

    Still, I think it's useful for go team to implement this function in next
    version of Go.
    No, this has been discussed in the past.
    even if we implement a isclosed() function for this task, using it will
    still involve inherent race.

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • James Bardin at Apr 19, 2013 at 1:45 pm
    What good would checking if a channel is closed be? Even if a check returns
    that a channel is open, it could be closed between the check and when you
    use it.


    On Friday, April 19, 2013 9:38:14 AM UTC-4, Reck Hou wrote:

    Please read this article first:


    http://stackoverflow.com/questions/16105325/how-to-check-a-channel-is-closed-or-not-without-reading-it


    "So I think the best way to solve this is worker() close channel when
    exits, if the controller finds a channel closed, it will jump over it and
    do nothing. But I can't find how to check a channel is already closed or
    not in this situation. If I try to read the channel in controller, the
    controller might be blocked. So I'm very confused for now."

    PS: Recovering the raised panic is what I have tried, but it will close
    goroutine which raised panic. In this case it will be controller so it's no
    use.

    Still, I think it's useful for go team to implement this function in next
    version of Go.
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Reck Hou at Apr 19, 2013 at 1:50 pm
    My thought was if a channel is closed, I will jump over & remove it. But I
    can't read it to check whether it's closed or not since it will be blocked
    and whole controller will halt in my case.

    I think I might need a new design about this after some discussion...

    在 2013年4月19日星期五UTC+8下午9时45分04秒,James Bardin写道:
    What good would checking if a channel is closed be? Even if a check
    returns that a channel is open, it could be closed between the check and
    when you use it.


    On Friday, April 19, 2013 9:38:14 AM UTC-4, Reck Hou wrote:

    Please read this article first:


    http://stackoverflow.com/questions/16105325/how-to-check-a-channel-is-closed-or-not-without-reading-it


    "So I think the best way to solve this is worker() close channel when
    exits, if the controller finds a channel closed, it will jump over it and
    do nothing. But I can't find how to check a channel is already closed or
    not in this situation. If I try to read the channel in controller, the
    controller might be blocked. So I'm very confused for now."

    PS: Recovering the raised panic is what I have tried, but it will close
    goroutine which raised panic. In this case it will be controller so it's no
    use.

    Still, I think it's useful for go team to implement this function in next
    version of Go.
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chris dollin at Apr 19, 2013 at 2:00 pm

    On 19 April 2013 14:50, Reck Hou wrote:

    My thought was if a channel is closed, I will jump over & remove it. But I
    can't read it to check whether it's closed or not since it will be blocked
    and whole controller will halt in my case.
    `x, ok = <- ch` does not block.

    There may be reasons this isn't what you actually want to
    do, but blocking isn't one of them.

    Chris

    --
    Chris "allusive" Dollin

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Péter Szilágyi at Apr 19, 2013 at 2:03 pm
    Yes it will.

    The solution is:

    select {
    case x, ok := <- ch
    // Do something
    default:
    // Non blocking part
    }

    On Fri, Apr 19, 2013 at 3:59 PM, chris dollin wrote:
    On 19 April 2013 14:50, Reck Hou wrote:

    My thought was if a channel is closed, I will jump over & remove it. But
    I can't read it to check whether it's closed or not since it will be
    blocked and whole controller will halt in my case.
    `x, ok = <- ch` does not block.

    There may be reasons this isn't what you actually want to
    do, but blocking isn't one of them.

    Chris

    --
    Chris "allusive" Dollin

    --
    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 [email protected].
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Péter Szilágyi at Apr 19, 2013 at 2:04 pm
    Btw, just a note, the above does not block, but obviously will consume an
    element from ch if available.

    On Fri, Apr 19, 2013 at 4:02 PM, Péter Szilágyi wrote:

    Yes it will.

    The solution is:

    select {
    case x, ok := <- ch
    // Do something
    default:
    // Non blocking part
    }

    On Fri, Apr 19, 2013 at 3:59 PM, chris dollin wrote:
    On 19 April 2013 14:50, Reck Hou wrote:

    My thought was if a channel is closed, I will jump over & remove it. But
    I can't read it to check whether it's closed or not since it will be
    blocked and whole controller will halt in my case.
    `x, ok = <- ch` does not block.

    There may be reasons this isn't what you actually want to
    do, but blocking isn't one of them.

    Chris

    --
    Chris "allusive" Dollin

    --
    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 [email protected].
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chris dollin at Apr 19, 2013 at 2:07 pm

    On 19 April 2013 15:02, Péter Szilágyi wrote:

    Yes it will.
    in response to my
    `x, ok = <- ch` does not block.

    I had to re-read the paragraph in the spec three times before I
    realised I had read it so drastically wrong.

    Thanks, Péter

    Chris

    --
    Chris "allusive" Dollin

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dan Kortschak at Apr 19, 2013 at 9:40 pm
    A long time ago the ,ok chan receive didn't block.
    On 19/04/2013, at 11:37 PM, "chris dollin" wrote:

    I had to re-read the paragraph in the spec three times before I
    realised I had read it so drastically wrong.
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at Apr 19, 2013 at 3:27 pm

    On Fri, Apr 19, 2013 at 9:50 PM, Reck Hou wrote:

    My thought was if a channel is closed, I will jump over & remove it. But I
    can't read it to check whether it's closed or not since it will be blocked
    and whole controller will halt in my case.
    so the hypothetical isclose() is used to guard against read/send from/to
    closed channel, but
    there is always chance that after isclose(ch) returned false, the worker
    closed ch just before
    your read/send.
    I think I might need a new design about this after some discussion...
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 19, '13 at 1:38p
activeApr 19, '13 at 9:40p
posts11
users7
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase