using System;
using System.Threading; /// <summary>
/// Provides lock-free atomic read/write utility for a <c>bool</c> value. 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>bool</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>
/// <remarks>
/// It's also important to note that <c>++</c> and <c>--</c> are never atomic, and one of the main reasons this class is
/// needed. I don't believe its possible to overload these operators in a way that is autonomous.
/// </remarks>
/// \author Matt Bolt
public class AtomicBoolean { private int _value; /// <summary>
/// Creates a new <c>AtomicBoolean</c> instance with an initial value of <c>false</c>.
/// </summary>
public AtomicBoolean()
: this(false) { } /// <summary>
/// Creates a new <c>AtomicBoolean</c> instance with the initial value provided.
/// </summary>
public AtomicBoolean(bool value) {
_value = value ? : ;
} /// <summary>
/// This method returns the current value.
/// </summary>
/// <returns>
/// The <c>bool</c> value to be accessed atomically.
/// </returns>
public bool Get() {
return _value != ;
} /// <summary>
/// This method sets the current value atomically.
/// </summary>
/// <param name="value">
/// The new value to set.
/// </param>
public void Set(bool 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 bool GetAndSet(bool 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(bool expected, bool result) {
int e = expected ? : ;
int r = result ? : ;
return Interlocked.CompareExchange(ref _value, r, e) == e;
} /// <summary>
/// This operator allows an implicit cast from <c>AtomicBoolean</c> to <c>int</c>.
/// </summary>
public static implicit operator bool(AtomicBoolean value) {
return value.Get();
} }

C# AtomicBoolean的更多相关文章

  1. AtomicBoolean使用

    使用 AtomicBoolean 高效并发处理 "只初始化一次" 的功能要求: 1 private static AtomicBoolean initialized = new A ...

  2. AtomicBoolean介绍与使用

       java.util.concurrent.atomic.AtomicBoolean 继承自Object. 介绍: 在这个Boolean值的变化的时候不允许在之间插入,保持操作的原子性 方法和举例 ...

  3. AtomicBoolean运用

    AtomicBoolean运用 首先先看如下例子 private static class BarWorker implements Runnable { private static boolean ...

  4. JAVA多线程两个实用的辅助类(CountDownLatch和AtomicBoolean)

    AtomicBoolean它允许一个线程等待一个线程完成任务,然后运行: A boolean value that may be updated atomically. See the java.ut ...

  5. Java AtomicBoolean (Java代码实战-008)

    值得一提的是,Java的AtomXXX类并不是使用了锁的方式进行同步,而是采用了一种新的理念,叫做CAS(Compare And Swap)CAS是一组CPU原语指令,用来实现多线程下的变量同步(原子 ...

  6. AtomicBoolean

    它的两种用法: 1.保证某段语句只执行一次. 首先我们要知道compareAndSet的作用,判断对象当时内部值是否为第一个参数,如果是则更新为第二个参数,且返回ture,否则返回false.那么默认 ...

  7. java并发编程:线程安全管理类--原子操作类--AtomicBoolean

    1.类AtomicBoolean

  8. Java并发包:AtomicBoolean和AtomicReference

      AtomicBoolean AtomicBoolean是一个读和写都是原子性的boolean类型的变量.这里包含高级的原子操作,例如compareAndSet().AtomicBoolean位于J ...

  9. juc原子类之二:基本类型原子类AtomicInteger(AtomicLong、AtomicBoolean)

    一.AtomicInteger简介 AtomicInteger, AtomicLong和AtomicBoolean这3个基本类型的原子类的原理和用法相似.以AtomicInteger对基本类型的原子类 ...

随机推荐

  1. apache的日志access_log分析

    正常日志格式:客户端地址 访问者的标识 访问者的验证名字 请求的时间 请求类型 请求的HTTP代码 发送给客户端的字节数   当网站出问题时分析日志,第一步一般都不会是看访问日志.但是也不能忽视它,在 ...

  2. 自定义页签logo

    1.webpack.prod.conf new HtmlWebpackPlugin({ filename: process.env.NODE_ENV === 'testing' ? 'index.ht ...

  3. Unity 3D游戏-见缝插针源码

    Unity见缝插针功能实现 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Sphere Rotatio ...

  4. 《DSP using MATLAB》Problem 3.9

    利用的频移性质为: 本习题代码: %% ------------------------------------------------------------------------ %% Outp ...

  5. jsp内置对象request 和response

    1.request对象主要用于处理客户端的请求 request对象常用方法 一.String  request.getParameter(String name)             根据页面表单 ...

  6. mac OS 安装 scikit-learn

    最近用来做实验,使用python时发现scikit-learn提供的库非常好用.因此,在电脑上果断下载安装: step1: sudo easy_install pip step2: sudo pip ...

  7. wpf的datepicker处理(转)

    如果有2个datepicker,控制时间起和止的话,可以把第二个datepicker加一个属性,DisplayDateStart = "{Binding SelectedDate,Eleme ...

  8. MySQL Transaction--快照读和当前读

    在MySQL读取数据时可以按照是否使用一致性非锁定读来分为快照读和当前读:1.快照读:MySQL使用MVCC (Multiversion Concurrency Control)机制来保证被读取到数据 ...

  9. sql 变量赋值

    mysql 的变量赋值如下: set @name='app' ; or set @name:='appfirst'; or with select select @appname:='you name ...

  10. hadoop 安装、命令

    hadoop安装步骤: 安装java 安装hadoop 下载地址:http://apache.claz.org/hadoop/common/ (说明:该网址current文件夹下,是最新版) hado ...