• C# Atomic<T> Generic


        using System;
        using System.Threading;
    
        /// <summary>
        /// Provides lock-free atomic read/write utility for a reference type, <c>T</c>, instance. The atomic classes found in this package
        /// were are meant to replicate the <c>java.util.concurrent.atomic</c> package in Java by Doug Lea. The two main differences
        /// are implicit casting back to the <c>T</c> data type, and the use of a non-volatile inner variable.
        /// 
        /// <para>The internals of these classes contain wrapped usage of the <c>System.Threading.Interlocked</c> class, which is how
        /// we are able to provide atomic operation without the use of locks. </para>
        /// </summary>
        /// author Matt Bolt
        public class Atomic<T> where T : class {
    
            private T _value;
    
            /// <summary>
            /// Creates a new <c>Atomic</c> instance with an initial value of <c>null</c>.
            /// </summary>
            public Atomic()
                : this(null) {
    
            }
    
            /// <summary>
            /// Creates a new <c>Atomic</c> instance with the initial value provided.
            /// </summary>
            public Atomic(T value) {
                _value = value;
            }
    
            /// <summary>
            /// This method returns the current value.
            /// </summary>
            /// <returns>
            /// The <c>T</c> instance.
            /// </returns>
            public T Get() {
                return _value;
            }
    
            /// <summary>
            /// This method sets the current value atomically.
            /// </summary>
            /// <param name="value">
            /// The new value to set.
            /// </param>
            public void Set(T value) {
                Interlocked.Exchange(ref _value, value);
            }
    
            /// <summary>
            /// This method atomically sets the value and returns the original value.
            /// </summary>
            /// <param name="value">
            /// The new value.
            /// </param>
            /// <returns>
            /// The value before setting to the new value.
            /// </returns>
            public T GetAndSet(T value) {
                return Interlocked.Exchange(ref _value, value);
            }
    
            /// <summary>
            /// Atomically sets the value to the given updated value if the current value <c>==</c> the expected value.
            /// </summary>
            /// <param name="expected">
            /// The value to compare against.
            /// </param>
            /// <param name="result">
            /// The value to set if the value is equal to the <c>expected</c> value.
            /// </param>
            /// <returns>
            /// <c>true</c> if the comparison and set was successful. A <c>false</c> indicates the comparison failed.
            /// </returns>
            public bool CompareAndSet(T expected, T result) {
                return Interlocked.CompareExchange(ref _value, result, expected) == expected;
            }
    
            /// <summary>
            /// This operator allows an implicit cast from <c>Atomic&lt;T&gt;</c> to <c>T</c>.
            /// </summary>
            public static implicit operator T(Atomic<T> value) {
                return value.Get();
            }
    
    }

    https://github.com/mbolt35/CSharp.Atomic
  • 相关阅读:
    代码签名证书原理和作用
    安装SSL证书有什么作用?
    有关如何修复Android手机上的SSL连接错误的快速指南
    可信时间戳如何生成?时间戳基本工作原理
    全球通用的数字证书产品选购指南
    国密SSL证书申请免费试用
    国密SSL证书免费试用申请指南
    哈希算法的原理和用途详解
    https证书安装无效的主要原因
    Anatomy of a Database System学习笔记
  • 原文地址:https://www.cnblogs.com/lenmom/p/10042832.html
Copyright © 2020-2023  润新知