Is it safe (i.e. race-free) to concurrently read/write or write/write a slice when you are certain the slice indices are different? I'm not worried about observability of the writes, just worried about corruption of the data.For example, the following (at http://play.golang.org/p/BOWoiHMH34) writes to both x[0] and x[1] in separate goroutines, and it is known that the indices are different. However, I suppose there could be word-size contention issues if a 64-bit machine is trying to write to "different" 8-bit addresses that are actually in the same 64-bit word.
func main() {x := make([]byte, 2)done := make(chan bool)go func() {x[0] = 7done <- true}()x[1] = 13<-donefmt.Print(x)}
--Thanks,John C.
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/groups/opt_out.
On Sat, Jan 4, 2014 at 9:04 AM, John C. <jscroc...@gmail.com> wrote:
Is it safe (i.e. race-free) to concurrently read/write or write/write a slice when you are certain the slice indices are different? I'm not worried about observability of the writes, just worried about corruption of the data.For example, the following (at http://play.golang.org/p/BOWoiHMH34) writes to both x[0] and x[1] in separate goroutines, and it is known that the indices are different. However, I suppose there could be word-size contention issues if a 64-bit machine is trying to write to "different" 8-bit addresses that are actually in the same 64-bit word.Most machine models, well x86, allow different cpu's to write different bytes our of a word at a severe penalty due to cache line thrashing.func main() {x := make([]byte, 2)done := make(chan bool)go func() {x[0] = 7done <- true}()x[1] = 13<-donefmt.Print(x)}There are two camps on this matter, I am in the camp that done <- true does not imply a happens before relationship such that <- done will see x[0] == 7. I do not recommend this, use a mutex.
On Fri, Jan 3, 2014 at 3:18 PM, Dave Cheney <da...@cheney.net> wrote:
On Sat, Jan 4, 2014 at 9:04 AM, John C. <jscroc...@gmail.com> wrote:
Is it safe (i.e. race-free) to concurrently read/write or write/write a slice when you are certain the slice indices are different? I'm not worried about observability of the writes, just worried about corruption of the data.For example, the following (at http://play.golang.org/p/BOWoiHMH34) writes to both x[0] and x[1] in separate goroutines, and it is known that the indices are different. However, I suppose there could be word-size contention issues if a 64-bit machine is trying to write to "different" 8-bit addresses that are actually in the same 64-bit word.Most machine models, well x86, allow different cpu's to write different bytes our of a word at a severe penalty due to cache line thrashing.func main() {x := make([]byte, 2)done := make(chan bool)go func() {x[0] = 7done <- true}()x[1] = 13<-donefmt.Print(x)}There are two camps on this matter, I am in the camp that done <- true does not imply a happens before relationship such that <- done will see x[0] == 7. I do not recommend this, use a mutex.I cannot see how to reconcile your "camp" with the Go memory model as currently published."A send on a channel happens before the corresponding receive from that channel completes." http://golang.org/ref/mem#tmp_6
I agree, however x is not sent through the done channel so it's contents are not guaranteed to be visible to the receiver of the value passed over done.