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. NBUT 1225 NEW RDSP MODE I 2010辽宁省赛

    Time limit  1000 ms Memory limit  131072 kB Little A has became fascinated with the game Dota recent ...

  2. Ajax请求数据的两种方式

    ajax 请求数据的两种方法,有需要的朋友可以参考下. 实现ajax 异步访问网络的方法有两个.第一个是原始的方法,第二个是利用jquery包的 原始的方法不用引入jquery包,只需在html中编写 ...

  3. 3.3 shell控制流结构

    shell中的控制流包括if then else语句,case语句,for循环,until循环,while循环,break控制,continue控制. 条件测试: 有时判断字符串是否相等或检查文件状态 ...

  4. ubuntu journalctl — 检索 systemd 日志

    常用: 查看最近1000行log sudo journalctl -f --lines=1000 -u server.$PROJECT_NAME --no-full, --full, -l 如果字段内 ...

  5. HDU 1232:畅通工程(并查集模板)

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. cf 295 div 2 B (bfs)

    题意:给出 n.m 两数,可以对 n 进行两种操作 减一或者乘二,操作过程中 n 必须保证非负,问使 n 变为 m 至少需要几步操作. 这是我练水题的时候做到的,题目不难,只是我 bfs 一直没怎么用 ...

  7. 关联容器set的用法(关联容器,红黑树,)

    set和multiset会根据特定的排序准则自动将元素排序,set中元素不允许重复,multiset可以重复.// 2017/7/23号 好像set容器里面只能装一个元素 #include<io ...

  8. IP地址 无限网卡的MAC地址

  9. 使用systemd严格保证启动顺序

    需求: 服务B要在服务A之后启动,且由于存在强内在依赖关系,B必须在A完成初始化之后才能被启动. 解决方法: 首先使用systemd,service脚本需要配置服务B要after服务A. 其次,A服务 ...

  10. ASM 磁盘、目录的管理

    --======================== -- ASM 磁盘.目录的管理 --======================== ASM磁盘是ASM体系结构的重要组成部分,ASM磁盘由ASM ...