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<T></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的更多相关文章
- 并发编程之原子操作Atomic&Unsafe
原子操作:不能被分割(中断)的一个或一系列操作叫原子操作. 原子操作Atomic主要有12个类,4种类型的原子更新方式,原子更新基本类型,原子更新数组,原子更新字段,原子更新引用.Atomic包中的类 ...
- Python Django,事务,transaction.atomic,事务保存点
from django.shortcuts import renderfrom django.http import HttpResponsefrom django.views.generic imp ...
- 并发编程之原子Atomic&Unsafe
1.原子更新基本类型类 用于通过原子的方式更新基本类型,Atomic包提供了以下三个类: AtomicBoolean:原子更新布尔类型. AtomicInteger:原子更新整型. AtomicL ...
- 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
- JUC学习笔记--Atomic原子类
J.U.C 框架学习顺序 http://blog.csdn.net/chen7253886/article/details/52769111 Atomic 原子操作类包 Atomic包 主要是在多线程 ...
- C#:泛型(Generic)
前言: 此系列都为个人对C#的回顾,属于个人理解,新司机可参考.求老司机指点.如果有什么问题或不同见解,欢迎大家与我沟通! 目录: 泛型是什么 泛型的好处及用途 如何声明使用泛型 泛型类 泛型方法 ...
- 原子类java.util.concurrent.atomic.*原理分析
原子类java.util.concurrent.atomic.*原理分析 在并发编程下,原子操作类的应用可以说是无处不在的.为解决线程安全的读写提供了很大的便利. 原子类保证原子的两个关键的点就是:可 ...
- Target runtime com.genuitec.runtime.generic.jee60 is not defined
转载自:http://jingyan.baidu.com/article/d7130635338e3f13fdf47518.html 用eclipse加载别人的工程,报错Target runtime ...
- 【iOS atomic、nonatomic、assign、copy、retain、weak、strong】的定义和区别详解
一.atomic与nonatomic 1.相同点 都是为对象添加get和set方法 2.不同点 atomic为get方法加了一把安全锁(及原子锁),使得方法get线程安全,执行效率慢 nonatomi ...
随机推荐
- Ubuntu:火狐浏览器加速下载(Flashgot+Aria2+Uget)
火狐浏览器加速下载 应用到的程序 Aria2 Uget firefox插件Flashgot 1.下载Uget sudo apt-get install uget 2. 下载Aria2 sudo apt ...
- 【Android 】查看被测应用程序package和launchable-activity
[appt] appt 工具所在位置..\sdk\build-tools\23.0.1\aapt.exe #查看Package>aapt dump badging C:\QQemail.apk ...
- Okhttp之CallServerInterceptor简单分析
在Okhttp源码分析专栏的几篇博客分析了Okhttp几个拦截器的主要功能,还剩下最后一个拦截器CallServerInterceptor没有分析,本篇博客就简单分析下该拦截器的功能. 在Okhttp ...
- 空格填充器(alignBySpace)
/******************************************************************* * 空格填充器(alignBySpace) * 声明: * 1 ...
- Pycharm出现的部分快捷键无效问题及解决办法
为了进行python开发,下载了Pycharm.但是发现启动后,执行ctrl+c和ctrl+v等快捷键都无法生效. 网上搜索了下,参考https://blog.csdn.net/c2366994582 ...
- Dlib与OpenCV图片转换
re: 1. https://zhuanlan.zhihu.com/p/36489663 2. https://stackoverflow.com/questions/38180410/convert ...
- Maven项目中的配置
1 在配置好Maven项目后,新建一个Maven Project.如图 点击next,会进入如下界面: 选择webapp,不选择默认的quickstart,之后会进入以下界面 填好Group ID 和 ...
- Java乱码解决之道
1.常见字符编码 ASCII编码: ASCII,American Standard Code for Information Interchange,是基于拉丁字母的一套电脑编码系统,主要用于显示现代 ...
- Python3中 sys.argv的用法
sys.avgr 是一个Python的引用模块.刚好做一个作业需要用到它,在sublime上编辑后运行,试图从结果发现它的用途,然而结果一直都是没结果. 后面在网上查了资料,才明白过来.sys.arg ...
- CTF-练习平台-Misc之 听首音乐
十九.听首音乐 用软件audacity打开后发现如下 上面那一行是可以看做摩斯电码,翻译过来是: ..... -... -.-. ----. ..--- ..... -.... ....- ----. ...