I almost went crazy when I tried to debug a random 40x performance drop when working in x86 according to an algorithm that makes heavy use of Interlock.CompareExchange with Int64.
I finally isolated the problem, and this only happened when the specified Int64 was not aligned by 8 bytes.
Regardless of how I explicitly put the field in StructLayout, it depends on the base address of the external object on the heap. On x86, the base address will be aligned by 4 bytes or aligned by 8 bytes.
I thought about defining a structure with 12 bytes and set Int64 with an offset of 0 or an offset of 4 depending on alignment, but this is kind of a hack.
Is there any good practice in C # to perform a blocking operation on Int64 in x86 that guarantees proper alignment?
EDIT
The code can be found here:
https://github.com/akkadotnet/akka.net/pull/1569#discussion-diff-47997213R520
Its a threadpool implementation based on Clr ThreadPool. The problem is to save the state of the user semaphore in the structure of 8 bytes and change it using InterlockedCompareExchange64.
source
share