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对基本类型的原子类 ...
随机推荐
- 关闭所有的screen
由于开了很多个screen同时工作,关闭是一个一个比较麻烦,写个命令在这以便日后想不起来时可以用到.1.先看看有多少个screen screen -ls |awk '/Socket/'|awk '{p ...
- 隔行扫瞄/逐行扫瞄的介绍(Interlaced / Progressive)
隔行扫瞄/逐行扫瞄的介绍(Interlaced / Progressive) 本篇不是着重在理论说明, 而是实际验証结果的分享, 所以只简略解释何谓交错与非交错, 请参考如后. 交错扫瞄(隔行扫瞄 ...
- centos7系统初始化
echo "# swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间" >> /etc/sysctl.conf echo -e "v ...
- Python中的import
模块(module):用来从逻辑(实现一个功能)上组织Python代码(变量.函数.类),本质就是*.py文件.文件是物理上组织方式"module_name.py",模块是逻辑上组 ...
- Spring DI
一. Spring DI 依赖注入 利用spring IOC实例化了对象,而DI将实例化的对象注入到需要对象的地方,完成初始化任务. 对象由spring创建,之后再由spring给属性赋值 spr ...
- Laravel学习之旅(三)
视图 一.怎么新建视图: 1.视图默认存放路径:resources/views: 2.laravel模板支持原生的PHP,直接可以在resources/views新建一个PHP文件,例如: index ...
- POJ 2441 Arrange the Bulls 状态压缩递推简单题 (状态压缩DP)
推荐网址,下面是别人的解题报告: http://www.cnblogs.com/chasetheexcellence/archive/2012/04/16/poj2441.html 里面有状态压缩论文 ...
- WPF开源界面库及控件
WPF开源项目 WPF有很多优秀的开源项目,我以为大家都知道,结果,问了很多人,其实他们不知道.唉,太可惜了! 先介绍两个比较牛逼的界面库 1.MaterialDesignInXamlToolkit ...
- Java Web HelloWorld!
距离上次做Java Web开发已经两年多了,我几乎忘得一干二净……都忘记咋搭建环境了……,然后Eclipse官网莫名其妙的挂掉.幸好电脑里还有份两年前的开发环境备份…… 重拾Java Web开发啊,说 ...
- MySQL--禁用账号和设置账号有效期
======================================================================= MySQL5.5/5.6版本 在MySQL 5.7 版本 ...