C# AtomicBoolean
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的更多相关文章
- AtomicBoolean使用
使用 AtomicBoolean 高效并发处理 "只初始化一次" 的功能要求: 1 private static AtomicBoolean initialized = new A ...
- AtomicBoolean介绍与使用
java.util.concurrent.atomic.AtomicBoolean 继承自Object. 介绍: 在这个Boolean值的变化的时候不允许在之间插入,保持操作的原子性 方法和举例 ...
- AtomicBoolean运用
AtomicBoolean运用 首先先看如下例子 private static class BarWorker implements Runnable { private static boolean ...
- JAVA多线程两个实用的辅助类(CountDownLatch和AtomicBoolean)
AtomicBoolean它允许一个线程等待一个线程完成任务,然后运行: A boolean value that may be updated atomically. See the java.ut ...
- Java AtomicBoolean (Java代码实战-008)
值得一提的是,Java的AtomXXX类并不是使用了锁的方式进行同步,而是采用了一种新的理念,叫做CAS(Compare And Swap)CAS是一组CPU原语指令,用来实现多线程下的变量同步(原子 ...
- AtomicBoolean
它的两种用法: 1.保证某段语句只执行一次. 首先我们要知道compareAndSet的作用,判断对象当时内部值是否为第一个参数,如果是则更新为第二个参数,且返回ture,否则返回false.那么默认 ...
- java并发编程:线程安全管理类--原子操作类--AtomicBoolean
1.类AtomicBoolean
- Java并发包:AtomicBoolean和AtomicReference
AtomicBoolean AtomicBoolean是一个读和写都是原子性的boolean类型的变量.这里包含高级的原子操作,例如compareAndSet().AtomicBoolean位于J ...
- juc原子类之二:基本类型原子类AtomicInteger(AtomicLong、AtomicBoolean)
一.AtomicInteger简介 AtomicInteger, AtomicLong和AtomicBoolean这3个基本类型的原子类的原理和用法相似.以AtomicInteger对基本类型的原子类 ...
随机推荐
- makefile for opencv
makefile #################################################### # Generic makefile - 万能Makefile # for ...
- CF1092(div3):Great Vova Wall (栈)(还不错)
D1. Great Vova Wall (Version 1): 题意:给定长度为N的墙,以及每个位置的一些高度,现在让你用1*2的砖和2*1的砖去铺,问最后能否铺到高度一样. 思路:分析其奇偶性,在 ...
- IIS7中Ajax.AjaxMethod无效的原因及解决方法
使用Ajax.AjaxMethod方法在asp.net的服务器下一切正常,用iis的时候,js中总是cs类找不到,具体的解决方法如下,遇到类似情况的朋友可以参考下 最近做用Ajax.AjaxMetho ...
- MySQL--区分表名大小写
============================================================================ 在MySQL中,可以通过lower_case_ ...
- StreamSets sdc rpc 测试
一个简单的参考图 destination pipeline 创建 pipeline flow sdc destination 配置 origin sdc rpc pipeline pipeline f ...
- 看懂Class文件的装载流程
Class文件的加载过程 ClassLoader的工作模式 类的热加载 1 Class文件的装载流程 只有被java虚拟机装载的Class类型才能在程序中使用(注意装载和加载的区别) 1.1 类装载的 ...
- 【转】解决Win7字体模糊不清晰的最佳办法
原文网址:http://blog.sina.com.cn/s/blog_3d5f68cd0100ldtp.html 相信初次用win7的朋友,都会遇到字体不清晰的问题,有很多人因为这个问题而放弃使用w ...
- vue-cli 项目构建性能分析工具
修改package.json { ... "scripts": { ... //新增 "analyz": "NODE_ENV=production n ...
- 线性模型的fit,predict
线性模型的fit其实一个进行学习的过程,根据数据和标签进行学习:predict则是基于fit之后形成的模型,来决定指定的数据对应于标签(y_train_5)的值. 下面的是手写字母判断是否为“5” s ...
- Scrapy下xpath基本的使用方法
Scrapy是基于python的开源爬虫框架,使用起来也比较方便.具体的官网档:http://doc.scrapy.org/en/latest/ 之前以为了解python就可以直接爬网站了,原来还要了 ...