I am doing some experimentation, and I need to atomically decrement a 16-bit (short) value in C#. The value is stored in unmanaged memory, and it is not a problem to ensure the value is aligned to a 16-bit virtual memory boundary.
I found that winnt.h (kernel32) exposes such a function: https://learn.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockeddecrement16
However, when I try to p/invoke this function from my .NET application, I get the following error:
Unable to find an entry point named 'InterlockedDecrement16' in DLL 'kernel32.dll'.
The method import code looks like this:
[DllImport("kernel32.dll")]
public static extern int InterlockedDecrement16(ref short addend);
Why am I getting this error, and do you see other alternatives to an interlocked16 p/invoke when there are no compiler intrinsics for this in C# (only 32-bit and 64-bit versions are exposed on Interlocked)