Atomic功能是提供简单的类保持原始值,并且提供对其执行原子操作;Atomic是线程安全的,类型的实现比较简单,就是通过各种措施保证变量的操作达到原子操作,有一点需要注意Atomic使用的时候只支持长度是32位或者64位的类或者类型,其他类型会出现问题。这里对类中用到的一些系统函数进行一些说明。

类型转换

 template <typename Dest, typename Source>
static inline Dest castTo (Source value) noexcept { union { Dest d; Source s; } u; u.s = value; return u.d; } static inline Type castFrom32Bit (int32 value) noexcept { return castTo <Type, int32> (value); }
static inline Type castFrom64Bit (int64 value) noexcept { return castTo <Type, int64> (value); }
static inline int32 castTo32Bit (Type value) noexcept { return castTo <int32, Type> (value); }
static inline int64 castTo64Bit (Type value) noexcept { return castTo <int64, Type> (value); }

以上类型转换比较巧妙,直接使用联合体进行两个变量的转换不用再对类型进行判断,Dest和Source占用的内存空间都位4或者8

以下对各个系统使用到的api做些说明
MAC:

原子操作

 #include <libkern/OSAtomic.h>
 int32_t
OSAtomicAdd32(int32_t theAmount, volatile int32_t *theValue); int32_t
OSAtomicAdd32Barrier(int32_t theAmount, volatile int32_t *theValue); int32_t
OSAtomicIncrement32(volatile int32_t *theValue); int32_t
OSAtomicIncrement32Barrier(volatile int32_t *theValue); int32_t
OSAtomicDecrement32(volatile int32_t *theValue); int32_t
OSAtomicDecrement32Barrier(volatile int32_t *theValue); int32_t
OSAtomicOr32(uint32_t theMask, volatile uint32_t *theValue); int32_t
OSAtomicOr32Barrier(uint32_t theMask, volatile uint32_t *theValue); int32_t
OSAtomicAnd32(uint32_t theMask, volatile uint32_t *theValue); int32_t
OSAtomicAnd32Barrier(uint32_t theMask, volatile uint32_t *theValue); int32_t
OSAtomicXor32(uint32_t theMask, volatile uint32_t *theValue); int32_t
OSAtomicXor32Barrier(uint32_t theMask, volatile uint32_t *theValue); int32_t
OSAtomicOr32Orig(uint32_t theMask, volatile uint32_t *theValue); int32_t
OSAtomicOr32OrigBarrier(uint32_t theMask, volatile uint32_t *theValue); int32_t
OSAtomicAnd32Orig(uint32_t theMask, volatile uint32_t *theValue); int32_t
OSAtomicAnd32OrigBarrier(uint32_t theMask, volatile uint32_t *theValue); int32_t
OSAtomicXor32Orig(uint32_t theMask, volatile uint32_t *theValue); int32_t
OSAtomicXor32OrigBarrier(uint32_t theMask, volatile uint32_t *theValue); int64_t
OSAtomicAdd64(int64_t theAmount, volatile int64_t *theValue); int64_t
OSAtomicAdd64Barrier(int64_t theAmount, volatile int64_t *theValue); int64_t
OSAtomicIncrement64(volatile int64_t *theValue); int64_t
OSAtomicIncrement64Barrier(volatile int64_t *theValue); int64_t
OSAtomicDecrement64(volatile int64_t *theValue); int64_t
OSAtomicDecrement64Barrier(volatile int64_t *theValue); bool
OSAtomicCompareAndSwapInt(int oldValue, int newValue,
volatile int *theValue); bool
OSAtomicCompareAndSwapIntBarrier(int oldValue, int newValue,
volatile int *theValue); bool
OSAtomicCompareAndSwapLong(long oldValue, long newValue,
volatile long *theValue); bool
OSAtomicCompareAndSwapLongBarrier(long oldValue, long newValue,
volatile long *theValue); bool
OSAtomicCompareAndSwapPtr(void* oldValue, void* newValue,
void* volatile *theValue); bool
OSAtomicCompareAndSwapPtrBarrier(void* oldValue, void* newValue,
void* volatile *theValue); bool
OSAtomicCompareAndSwap32(int32_t oldValue, int32_t newValue,
volatile int32_t *theValue); bool
OSAtomicCompareAndSwap32Barrier(int32_t oldValue, int32_t newValue,
volatile int32_t *theValue); bool
OSAtomicCompareAndSwap64(int64_t oldValue, int64_t newValue,
volatile int64_t *theValue); bool
OSAtomicCompareAndSwap64Barrier(int64_t oldValue, int64_t newValue,
volatile int64_t *theValue); bool
OSAtomicTestAndSet(uint32_t n, volatile void *theAddress); bool
OSAtomicTestAndSetBarrier(uint32_t n, volatile void *theAddress); bool
OSAtomicTestAndClear(uint32_t n, volatile void *theAddress); bool
OSAtomicTestAndClearBarrier(uint32_t n, volatile void *theAddress); bool
OSSpinLockTry(OSSpinLock *lock); void
OSSpinLockLock(OSSpinLock *lock); void
OSSpinLockUnlock(OSSpinLock *lock); void
OSAtomicEnqueue(OSQueueHead *list, void *new, size_t offset); void*
OSAtomicDequeue(OSQueueHead *list, size_t offset);

LINUX:

原子操作

type __sync_fetch_and_add (type *ptr, type value);
type __sync_fetch_and_sub (type *ptr, type value);
type __sync_fetch_and_or (type *ptr, type value);
type __sync_fetch_and_and (type *ptr, type value);
type __sync_fetch_and_xor (type *ptr, type value);
type __sync_fetch_and_nand (type *ptr, type value);
type __sync_add_and_fetch (type *ptr, type value);
type __sync_sub_and_fetch (type *ptr, type value);
type __sync_or_and_fetch (type *ptr, type value);
type __sync_and_and_fetch (type *ptr, type value);
type __sync_xor_and_fetch (type *ptr, type value);
type __sync_nand_and_fetch (type *ptr, type value);

WINDOWS:

原子操作

LONG InterLockedIncrement(
  LPLONG lpAddend // variable address
  );
LONG InterlockedDecrement(
  LPLONG lpAddend // variable address
  ); LONG__cdeclInterlockedExchangeAdd(
_Inout_LONGvolatile*Addend,
_In_LONGValue
); LONG InterlockedCompareExchange(
LPLONG Destination, LONG Exchange, LONG Comperand );
PVOID InterlockedCompareExchangePointer(
PVOID *Destination, PVOID Exchange, PVOID Comperand );

总体的原子操作完全是依赖于系统API来实现的,留一下相关系统的原子操作api即可

一起学JUCE之Atomic的更多相关文章

  1. 一起学JUCE之HashMap

    基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同.) ...

  2. juce中的内存泄漏检测

    非常值得借鉴的做法,基于引用计数和局部静态变量,代码比较简单不加详解. //============================================================== ...

  3. juce中的引用计数

    这个类提供了最基本的引用计数管理,界面库中,经常都需要消息发送,而带来的后果就是不知道消息中包含的对象是否还存在,如果不能很好管理的话就容易出现访问销毁了的对象这样的情况,所以,juce的界面无素也基 ...

  4. 还在用Synchronized?Atomic你了解不?

    前言 只有光头才能变强 之前已经写过多线程相关的文章了,有兴趣的同学可以去了解一下: https://github.com/ZhongFuCheng3y/3y/blob/master/src/thre ...

  5. 图学java基础篇之并发

    概述 并发处理本身就是编程开发重点之一,同时内容也很繁杂,从底层指令处理到上层应用开发都要涉及,也是最容易出问题的地方.这块知识也是评价一个开发人员水平的重要指标,本人自认为现在也只是学其皮毛,因此本 ...

  6. Juce源代码分析(九)应用程序基类ApplicationBase

    在前面的几篇文章,分析的都是Juce库里面Core模块的内存部分,除了骨灰级C++爱好者之外,貌似大家对这些都不是非常感兴趣.相信大家更想知道Juce是怎么用于产品开发,而对于它的构成不是非常感兴趣. ...

  7. 跟着阿里p7一起学java高并发 - 第18天:玩转java线程池,这一篇就够了

    java中的线程池,这一篇就够了 java高并发系列第18篇文章. 本文主要内容 什么是线程池 线程池实现原理 线程池中常见的各种队列 自定义线程创建的工厂 常见的饱和策略 自定义饱和策略 线程池中两 ...

  8. 跟着阿里p7一起学java高并发 - 第19天:JUC中的Executor框架详解1,全面掌握java并发核心技术

    这是java高并发系列第19篇文章. 本文主要内容 介绍Executor框架相关内容 介绍Executor 介绍ExecutorService 介绍线程池ThreadPoolExecutor及案例 介 ...

  9. 学Python必背的初级单词,你都背了吗?

    今天给大家分享一些学习Python必须认识的英文单词,同时也是学习编程都必须会的单词,新手赶快学起来!有点长耐心看完. 小编推荐一个学Python的学习裙:九三七六六七 五零九,无论你是大牛还是小白, ...

随机推荐

  1. 【转载】GDI 映像方式 之 SetViewportOrgEx 与 SetWindowOrgEx 解析

    SetViewportOrgEx 与 SetWindowOrgEx 解析 这两个函数,用来改变视端口和窗口的原点,并都具有改变轴的效果,以致(0,0)不再指左上角. 「视端口」是依据设备坐标(图素)的 ...

  2. HDU-1232--畅通工程(最小生成树)

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...

  3. 使用RPM包安装、配置和拆卸MySQL

    通过rpm包安装.配置及卸载mysql的详细过程. 以MySQL-server-4.0.14-0.i386.rpm为例,放在/usr/src目录下 cd /usr/src rpm -ivh MySQL ...

  4. 河南多校联合训练 F 不是匹配

    描述  有N个人,N个活动, 每个人只会对2个或者3个活动感兴趣,  每个活动也只有两个人或者两个活动对它兴趣,每个人参加一个  感兴趣的活动需要一天 ,且当天该活动被参加时,其他的人不能参加  如果 ...

  5. java反射简解

    1.首先一个问题,什么是类,类是不是对象? 我们总是说我们new一个对象出来 那么我们还没有new的时候,建造的那个java类是不是对象呢? 是 它是java.lang.Class的对象 对于反射我们 ...

  6. 一个appium 博客

    http://www.cnblogs.com/tobecrazy/category/699177.html appium Java控制Appium server start/stop 摘要: 相信很多 ...

  7. Linux 下安装配置 JDK1.7

    目录[-] 1.下载JDK 2.解压安装 3.配置环境变量 4.配置默认JDK(一般情况下这一步都可以省略) 5.测试 1.下载JDK Linux操作系统是:Centos6.5-x64 目前最新的JD ...

  8. netty最快?

    http://www.techempower.com/blog/2013/04/05/frameworks-round-2/

  9. 使用FusionCharts出柱状图和饼状图

    在最近的项目中,需要使用出图,能够查看柱状图,饼状图等效果,刚开始我们用JS写的效果,发现效果不理想,找了一个JS插件发现效果还是不理想,客户也不满意,客户希望要很炫的效果,最后我们使用了Fusion ...

  10. UIView你知道多少

    转载自:http://www.cnblogs.com/likwo/archive/2011/06/18/2084192.html   曾经有人这么说过,在iphone里你看到的,摸到的,都是UIVie ...