I was wondering if there’s a windows API to set a 16-byte array atomically?
I would really hate to introduce critical sections or mutexes into this just to implement this operations, so I’m trying to find an easier solution.
PS. I need this because this 16-byte array can be written into from a worker thread, and mostly read from a main thread.
All of the following assumes a 64-bit x86_64 architecture. I believe that in general what you asked for is not possible on 32-bit x86.
There are really two options. The first one is
_InterlockedCompareExchange128, which translates toLOCK CMPXCHG16B. To duplicate the functionality of a 16-byte store with it, you’d need to do something like this:Note that because this is an interlocked operation, it implies the equivalent of a
_ReadWriteBarrier. This is normally what is meant by an “atomic” operation.If you want a pure store, not a compare-exchange, then you can use anything that translates to an aligned 16-byte store on the hardware. For example, the
MOVDQAinstruction would qualify. In C++, that’d look something like this:Unless you have a reason to avoid SSE instructions, I think the second version might perform better. You also may want to decide if you want to relax the two memory barriers depending on your actual needs.