On 02/03/16 18:51, Sam Whited wrote:
> TL;DR — Senders close channels, receivers check if they're closed. If
> you're the sender and you send on a closed channel we panic because
> you're doing it wrong; it's effectively an assertion that makes sure
> you're using the pattern the way it was intended to be used.
What when there are several senders?
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Wed, Mar 2, 2016 at 12:58 PM, Tapio Raevaara <tapio.r...@iki.fi> wrote:
> I have to admit that I've always wondered why send and receive operations on
> nil channel don't simply panic. Am I overlooking something obvious here?
> When is permanent blocking *ever* desirable behaviour in this case?
I'm assuming that by "nil" channel you mean one that has nothing in it?
select {
case <-ch:
break
}
select {
case <-ch:
break
case <-(chan int(nil)):
}
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Have a look back in the git history to the point when send and receive used to work like this. There is lots of historical discussion, pre go 1.0, that explains why things are the way they are.
It seems like lock contention is a major pain point with channels in Go. What would some potential solutions be? I don't suppose a lock-free implementation is feasible.
On 3 Mar 2016 12:00 p.m., "tsuna" <tsun...@gmail.com> wrote:
>
> Maybe I’m blind but I didn’t find a clear explanation in any of those
> links as to why sending to / receiving from a nil channel (outside a
> select) was made to hang forever (and leak the calling goroutine)
> rather than crash the program with a panic?
>
It's specifically about making the behaviour of channels orthogonal to the select feature. Channel operations act the same whether they are in a select.
The benefit of this is that changes to how channels work don't require special cases in how select works.
Select only cares about whether a channel operation can proceed. New versions of Go can add additional operations to channels and select will still work the same as long as those operations maintain the interface of being able to proceed or not.
Review slides 24-27 of John Graham-Cummings presentation at Gohpercon 2014Hint: if you are ever using a channel outside of a select in production code, you are probably doing it wrong.You risk deadlock if you try to gracefully shutdown that process.
No, because range checks if the channel is closed. Equivalent for/select looks like this:
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/LM648yrPpck/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
A conditional yes: if you are writing a server process that needs to be able to shut down gracefully, then you should never range over a channel, because it can deadlock the shutdown. All your channel communication needs to be inside a select loop, where one of the options is receiving on the "shutdown request" channel.
On Thu, Mar 3, 2016, at 03:40 PM, Jason E. Aten wrote:
A conditional yes: if you are writing a server process that needs to be able to shut down gracefully, then you should never range over a channel, because it can deadlock the shutdown. All your channel communication needs to be inside a select loop, where one of the options is receiving on the "shutdown request" channel.
Forbidding the use of range here seems strange when there are so many other ways to deadlock a server process. It's easy enough to exit a range loop by use of a shared atomic bool or similar. I don't understand these kind of sweeping rules that forbid useful language constructs.
--
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...@googlegroups.com.
What sorcery is this? Clearly both receive statements can proceed, so one should be chosen at random, no?