fatal error: all goroutines are asleep - deadlock!
goroutine 1 [sleep]:
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:307
time.Sleep(0x3e8, 0x0)
/usr/local/go/src/runtime/time.go:105 +0x1c0
main.main()
/tmp/sandbox327672725/prog.go:81 +0x140
goroutine 5 [sleep]:
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:307
time.Sleep(0x1, 0x0)
/usr/local/go/src/runtime/time.go:105 +0x1c0
main.producer(0x430150, 0x40a0e0)
/tmp/sandbox327672725/prog.go:49 +0x60
created by main.main
/tmp/sandbox327672725/prog.go:73 +0xe0
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMV2RqpyK54ZsUJAPF%3D8Jk2GUjAXL%2B6E5-htX7R-wSuM_6MNVg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/F957AAC7-A4F9-45D5-A80E-2BA788C6C721%40ix.netcom.com.
--
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/AC9DEEBD-37DF-49FA-8171-E45418584697%40ix.netcom.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/C248F20A-88EC-4C36-9931-C9F35585206A%40ix.netcom.com.
Btw, the removing of the GOMAXPROCS causes things to execute serially- which is fine according to the spec - but it does make demonstrating certain concurrency structs/problems pretty difficult.
Unless something's changed recently, all programs in the playground execute without parallelism, so setting GOMAXPROCS shouldn't have any effect. The fact that it does have an affect appears to be a bug. I'd suggest reporting it as an issue.
As I pointed out in another reply, I am fairly certain the atomic operations must be valid in these cases due to the happens before relationship of them and mutexes.
In that other reply:
You can simply validate it by run: go run -race main.go for you program: https://play.golang.org/p/JRSEPU3Uf17
Not true. The race detector does not detect certain cases and can overreport.
Firstly, AFAIK there is no implied happens-before relationship between a non-atomic write to a variable and an atomic read from a variable, because there is no synchronization event associated with the atomic read.
Secondly, I am aware that the race detector can give false negatives in some cases, but it should not provide false positives AFAIK. There's only one such case that I'm aware of, and that's quite a different issue.
Given that the race detector reports a race in this very clear and simple case, ISTM that your program is wrong.
From https://golang.org/ref/mem:
If you must read the rest of this document to understand the behavior of your program, you are being too clever.
Don't be clever.
Also, there's really no point in the atomic read. You could implement the Read method as follows:
func (c *MultiWriterIntChan) Read() (int, bool) { x, ok := <-c.ch return x, ok
}
That is, let the close state flow naturally downstream via the channel. That way it will still work correctly if you start to use a buffered channel, for example.
I don’t agree that returning the channel is a proper design, it breaks the encapsulation. Technically mine could use multiple channels behind the scenes - yours cannot.
There's a trade-off here. By encapsulating in a method, you make it harder to wait for values in combination with other events (you'd need to create a goroutine to call your Read method and send the result to a channel, adding buffering, context switch overhead, and more state that needs to be explicitly shut down).
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAJhgaci57DOYHOk7C%3DjM8B6wenCxYv4JvNn2%3D13OGcd66Yc3EA%40mail.gmail.com.
(you’re comments were quoted, but I think I am replying correctly).The memory barriers provided by the Write Lock, and release will force the flush to memory of all mutations before the flush (the closed mutation) - meaning the atomic read will read the correct value.
There is a chance that a fully specified memory model could make that not be possible, but in the current de-facto memory model it is going to be the case on any CPU architecture I’m aware of. Some transactional memory systems might be able to avoid the flush on the ‘closed’, but unlikely.I would welcome you writing a test case to demonstrate this not being the case and failing.
The reason the race detector reports a race is because it expects all access to be guarded by the same guard, the Lock/Release must perform the same memory barriers on the SMP systems I’m aware of.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1753A5A2-5642-49A7-8815-5CC727E67F16%40ix.netcom.com.