Say I have two go routines:
var sequence int64
// writer
for i := sequence; i < max; i++ {
doSomethingWithSequence(i)
sequence = i
}
// reader
for {
doSomeOtherThingWithSequence(sequence)
}
So can I get by without atomic
?
Some potential risks I can think of:
reorder (for the writer, updating
sequence
happens beforedoSomething
) could happen, but I can live with that.sequence
is not properly aligned in memory so the reader might observe a partially updatedi
. Running on (recent kernel) linux with x86_64, can we rule that out?go compiler 'cleverly optimizes' the reader, so the access to
i
never goes to memory butcached
in a register. Is that possible in go?Anything else?