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. net start mysql启动mysql,提示发生系统错误 5 拒绝访问 解决方法

    解决问题方法如下: 在dos下运行net  start mysql 不能启动mysql!提示发生系统错误 5:拒绝访问!切换到管理员模式就可以启动了.所以我们要以管理员身份来运行cmd程序来启动mys ...

  2. 【opencv基础】detectmultiscale函数详解

    前言 简单的人脸检测程序可以直接基于opencv的函数库进行实现,本文介绍一下detectMultiScale函数. 函数简介 opencv2人脸检测使用的是detectMultiScale函数,可以 ...

  3. TP框架连接mongodb报错及解决办法

    mongodb版本3.4.7 1.认证错误:Failed to connect to: localhost:27017: Authentication failed on database 'test ...

  4. Linux配置NTP服务器,时间同步

    当服务器多了,时间准确与否,一致与否是个大问题.虽然这个问题总是被忽略,但是统一一致的时间是很有必要的.下面说一下在局域网内配置Linux时间服务器的方法. 配置的环境及要求: 假设在192.168. ...

  5. drill 集成开源s3 存储minio

    drill 支持s3数据的查询,同时新版的通过简单配置就可以实现minio 的集成 测试使用docker 运行drill 参考 https://www.cnblogs.com/rongfenglian ...

  6. EditPLus添加到右键图文教程

    最近在研究asp听他们说EditPlus非常适合,于是下了一个,感觉还真不错,EditPlus就是一个文本编辑器,说得通俗点他和WINDOWS自带的记事本差不多,但是功能更强,一般应用于程序员编程,因 ...

  7. 未能正确加载“VSTS for Database Professionals Sql Server Data-tier Application”包。(转)

    今天费了九牛二虎之力,重转好了vs2010之后,打开解决方案,报出下面的错误: ---------------------------Microsoft Visual Studio---------- ...

  8. java 物理资源回收 finally与try

    java垃圾回收机制不会回收任何物理资源(磁盘文件.数据库连接.网络连接),垃圾回收机制只能回收堆内存中对象所占用的内存. 方法一使用finally块,在finally块中写入资源回收代码,如下: p ...

  9. Mongodb基础用法及查询操作

    插入多条测试数据> for(i=1;i<=1000;i++){... db.blog.insert({"title":i,"content":&qu ...

  10. 【python】globle的使用

    python中直接定义的变量就是本地变量,使用global定义的变量就是全局变量.比如: a = 1 b = 1 def foo1(): global b #申明使用全局b a = 2 #a是本地变量 ...