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

C# Atomic<T> Generic的更多相关文章

  1. 并发编程之原子操作Atomic&Unsafe

    原子操作:不能被分割(中断)的一个或一系列操作叫原子操作. 原子操作Atomic主要有12个类,4种类型的原子更新方式,原子更新基本类型,原子更新数组,原子更新字段,原子更新引用.Atomic包中的类 ...

  2. Python Django,事务,transaction.atomic,事务保存点

    from django.shortcuts import renderfrom django.http import HttpResponsefrom django.views.generic imp ...

  3. 并发编程之原子Atomic&Unsafe

    1.原子更新基本类型类   用于通过原子的方式更新基本类型,Atomic包提供了以下三个类: AtomicBoolean:原子更新布尔类型. AtomicInteger:原子更新整型. AtomicL ...

  4. 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)

    前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...

  5. JUC学习笔记--Atomic原子类

    J.U.C 框架学习顺序 http://blog.csdn.net/chen7253886/article/details/52769111 Atomic 原子操作类包 Atomic包 主要是在多线程 ...

  6. C#:泛型(Generic)

    前言:  此系列都为个人对C#的回顾,属于个人理解,新司机可参考.求老司机指点.如果有什么问题或不同见解,欢迎大家与我沟通! 目录:  泛型是什么 泛型的好处及用途 如何声明使用泛型 泛型类 泛型方法 ...

  7. 原子类java.util.concurrent.atomic.*原理分析

    原子类java.util.concurrent.atomic.*原理分析 在并发编程下,原子操作类的应用可以说是无处不在的.为解决线程安全的读写提供了很大的便利. 原子类保证原子的两个关键的点就是:可 ...

  8. Target runtime com.genuitec.runtime.generic.jee60 is not defined

    转载自:http://jingyan.baidu.com/article/d7130635338e3f13fdf47518.html 用eclipse加载别人的工程,报错Target runtime ...

  9. 【iOS atomic、nonatomic、assign、copy、retain、weak、strong】的定义和区别详解

    一.atomic与nonatomic 1.相同点 都是为对象添加get和set方法 2.不同点 atomic为get方法加了一把安全锁(及原子锁),使得方法get线程安全,执行效率慢 nonatomi ...

随机推荐

  1. 如何修改magento产品详细页面的栏目

    magento默认模板里面的产品信息页面的布局是以两栏带右侧栏显示的,那么如何修改为两栏带左侧栏或者三栏.一栏的方式显示呢?下面教大家一种很简单的方法就可以实现.下面是默认的布局预览:

  2. Appium笔记(二) 丶Appium的安装

    一.前言 Appium 中有个很重要的组件Appium-Server,它主要用来监听我们的移动设备(真机或模拟器),然将不同编程语言编写的 appium 测试脚本进行解析,然后,驱动移动设备来运行测试 ...

  3. (5)可变、不可变和hash函数

    分类情况 与列表相似,列表用[],元组是()表示 内存角度看列表与数字的变与不变 列表 >>>l = [1,2,3,4] >>>id(l) 4392665160 & ...

  4. hdu1081 DP类最大子段和(二维压缩+前缀和数组/树状数组计数)

    题意:给出一个 n * n 的数字矩阵,问最大子矩阵和是多少. 由于和最长子段和问题类似,一开始想到的就是 DP ,一开始我准备用两个循环进行 DP ,对于每一个 (i,j) ,考察(i - 1,j) ...

  5. LG4454 【[CQOI2018]破解D-H协议】

    先谈一下BSGS算法(传送门) 但是上面这位的程序实现比较繁琐,看下面这位的. clover_hxy这样说 bsgs算法,又称大小步算法(某大神称拔山盖世算法). 主要用来解决 A^x=B(mod C ...

  6. Servlet基本操作

    一.Servlet的请求流程 web项目中的web.xml文件配置为: <servlet> <!--别名--> <servlet-name>Hello</se ...

  7. WPF中使用BitmapImage处理图片文件(转)

    (1)图片从文件导入 BitmapImage image = new BitmapImage(); image.BeginInit(); image.UriSource = new Uri(filen ...

  8. oracle 获取星期日期

    oracle 中的计算如下: 计算本星期的起始结束日期--得到星期一的日期select trunc(sysdate,''DD'')-to_char(sysdate,''D'')+2 from dual ...

  9. Win8被禁购信息战由暗到明

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jhzyz/article/details/26629277 冯强/文 关于中国政府禁止採购微软Win ...

  10. System帐户!我使用你登陆

    大家知道,SYSTEM是至高无上的超级管理员帐户.默认情况下,我们无法直接在登录对话框上以SYSTEM帐户的身份登录到Windows桌面环境.实际上SYSTEM帐户早就已经“盘踞”在系统中了.想想也是 ...