Code Monkey home page Code Monkey logo

locktype's Introduction

AtomicLock Class

Overview

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.

Features

  • 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.

Usage

Instantiation - Example 1

AtomicLock<int> atomicInt = new AtomicLock<int>(initialValue);

Instantiation - Example 2

AtomicLock<int> atomicInt = 0;

Performing Atomic Operations

atomicInt++;  // Atomic increment
atomicInt--;  // Atomic decrement
// And a lot of other pre-defined operations
// (Not AOT friendly - Use ExecuteOperations instead)

Executing Custom Operations

atomicInt.ExecuteOperation(x => x * 2); // Custom operation

Thread-Safe Value Update

atomicInt.UpdateValue(newValue);

Example code in C#

using LockType;
using static System.Console;

AtomicLock<int> locked = 0;

locked++; // <-- Thread Safe
WriteLine(locked);
locked++; // <-- Thread Safe
WriteLine(locked);

ReadKey(false);

Notes

  • 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.

License

This project is licensed under the MIT License.

locktype's People

Contributors

fabian2000 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.