AtomicLock<T>: Thread-safe operations for .NET, enabling atomic access and manipulation of shared data.
$ dotnet add package LockType-AtomicLockThe AtomicLock<T> class is a thread-safe structure designed to encapsulate a value and provide atomic operations on it. This class ensures that operations on the encapsulated value are performed safely in a multithreaded environment, preventing race conditions and ensuring consistency.
++), decrement (--), and basic comparison operators.AtomicLock<int> atomicInt = new AtomicLock<int>(initialValue);
AtomicLock<int> atomicInt = 0;
atomicInt++; // Atomic increment
atomicInt--; // Atomic decrement
// And a lot of other pre-defined operations
// (Not AOT friendly - Use ExecuteOperations instead)
atomicInt.ExecuteOperation(x => x * 2); // Custom operation
atomicInt.UpdateValue(newValue);
using LockType;
using static System.Console;
AtomicLock<int> locked = 0;
locked++; // <-- Thread Safe
WriteLine(locked);
locked++; // <-- Thread Safe
WriteLine(locked);
ReadKey(false);
dynamic for some operations, which may have performance implications in certain scenarios.dynamic.This project is licensed under the MIT License.