You can prevent the false sharing by using additional memory and putting gaps between the elements.
You need to use atomics even if only written by a single if there is a collating routine that is reading the values. > On Feb 1, 2019, at 6:37 AM, Marvin Renich <m...@renich.org> wrote: > > * johnmr...@gmail.com <johnmr...@gmail.com> [190131 23:50]: >>> Writing to adjacent memory locations [i.e. different array elements, from >>> different goroutines] will cause false sharing between CPU caches. This is >>> a performance, not a correctness issue. >> >> I'm looking to make an array of thread-safe counters - each counting a >> different thing. I propose to use an ordinary array, of fixed size, with >> thread safety coming solely from use of atomic operations to increment the >> values. Will it be correct, but prone to false sharing between caches? > > Your method sounds like it will be correct. > > If a single array element is only ever accessed from a single goroutine, > then the atomic operations will not be necessary, and whether you use > atomics or not, there will be false cache sharing. > > If a single array element is ever accessed by more than one goroutine, > then the atomics are necessary, and there will be necessary cache > sharing. > > ...Marvin > > -- > 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+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.