The 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.
- Thread-Safety: Ensures that all operations on the value are thread-safe.
- Atomic Operations: Supports atomic operations like increment (
++
), decrement (--
), and basic comparison operators. - Flexibility: Allows executing custom operations on the value through a provided delegate.
- Easy to Use: Designed with simplicity in mind, offering an intuitive API.
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);
- The class uses
dynamic
for some operations, which may have performance implications in certain scenarios. - Exception handling is integrated for runtime errors, especially for operations involving
dynamic
. - AOT developers should use ExecuteOperations Method instead of the Atomic Operations.
- Accessing the value by casting it to the original type is possible, but could not be thread safe.
This project is licensed under the MIT License.