On Sun, Sep 28, 2014 at 12:28 PM, Shuhei Tanuma <
chob...@gmail.com> wrote:
> Although, I'd like to swap struct member which type is interface.
> As I guess atomic.SwapPointer has several advantages over mutex.Lock (no
> need to lock every time...)
The largest object that can be atomically swapped using a single [bus
lock prefixed] CPU instruction is a machine word (in the first
approximation). Interface is larger than a machine word, so no, you
cannot do that. OTOH a pointer is a machine word so you can
semantically swap anything regardless of its size when you atomically
swap pointers to that things and always access them through
dereferencing that (volatile) pointer. The sync/atomic package
provides all of that.
In case importing package unsafe is a show stopper one can invent
something like a struct with fields [2]MyInterface and an atomically
changed index, for example.
-j