DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="2.7.0" />

 AtomicEnum

public static class AtomicEnum
Provides basic atomic operations for arbitrary enum type.
using System; using System.Runtime.CompilerServices; using System.Threading; namespace DotNext.Threading { public static class AtomicEnum { [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static TEnum VolatileRead<TEnum>(ref TEnum value) where TEnum : struct, Enum { if (sizeof(TEnum) != 8) return value; long num = Volatile.Read(ref *(long*)(&value)); return *(TEnum*)(&num); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static void VolatileWrite<TEnum>(ref TEnum value, TEnum newValue) where TEnum : struct, Enum { if (sizeof(TEnum) != 8) value = newValue; else Volatile.Write(ref *(long*)(&value), *(long*)(&newValue)); } } }