zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    python奇技淫巧——max/min函数的用法
    mysql中InnoDB存储引擎的行锁和表锁
    supervisor进程管理工具
    Tornado源码分析 --- Cookie和XSRF机制
    Tornado源码分析 --- Redirect重定向
    Tornado源码分析 --- 静态文件处理模块
    Tornado源码分析 --- Etag实现
    Python环境管理--virtualenvwrapper
    记录
    由 '' in 'abc' return True 引发的思考----Python 成员测试操作
  • 原文地址:https://www.cnblogs.com/lenmom/p/10042832.html
Copyright © 2011-2022 走看看