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. Java中域 实例域 静态域

    1.java中的域 所谓的域,翻译成英文就是field, 也就是我们常说的字段,或者说是属性. 比如类的字段(属性),局部的,全局的.所谓域,其实是“field”的翻译 然后实例域,就是 实例(&qu ...

  2. 转:Web安全与Rational AppScan入门

    Web 应用的基础概念 在讨论 Web 应用安全之前,先简单介绍一下 Web 应用基础概念,这样便于理解为什么 Web 应用是脆弱的,容易受到攻击. 1. 什么是 Web 应用 Web 应用是由动态脚 ...

  3. 【啊哈!算法】算法6:只有五行的Floyd最短路算法

            暑假,小哼准备去一些城市旅游.有些城市之间有公路,有些城市之间则没有,如下图.为了节省经费以及方便计划旅程,小哼希望在出发之前知道任意两个城市之前的最短路程.         上图中有 ...

  4. webapp之路--meta标签format-detection、apple-mobile-web-app-capable

    1. format-detection翻译成中文的意思是“格式检测”,顾名思义,它是用来检测html里的一些格式的,那关于meta的format-detection属性主要是有以下几个设置: meta ...

  5. hdu_5691_Sitting in Line(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5691 题意:中文,不解释 题解:设dp[i][j]表示当前状态为i,以第j个数为末尾的最忧解,然后dp ...

  6. Kyoto Cabinet--nosql型单机数据库

    摘要: Kyoto Cabinet是轻量级nosql型本地内存数据库 简介 Kyoto Cabinet是一个数据库管理的 lib,是 Tokyo Cabinet 的改进版本.数据库是一个简单的包含记录 ...

  7. javascript 之Function对象的apply(),call(),bind(),方法和arguments,caller,length属性

    注:这篇文章原文:http://www.jb51.net/article/30883.htm 自己作为学习,重新写写. 一.写在前面的话 前端javascript编程还只是略懂皮毛,DOM知道一点,j ...

  8. android脚步---如何看log之程序停止运行,和UI线程和非UI线程之间切换

    经常运行eclipse时,烧到手机出现,“停止运行”,这时候得通过logcat查log了.一般这种情况属于FATAL EXCEPTION,所以检索FATAL 或者 EXCEPTION,然后往下看几行 ...

  9. 关于Application.Lock…Application.Unlock有什么作用?

    因为Application变量里一般存储的是供所有连接到服务器的用户共享的信息(就像程序中所说的 "全局变量 "), 由于是全局变量,所以就容易出现两个或者多个用户同时对这一变量进 ...

  10. FTP 1.0

    自己写的可以实现文件的下载(必须自己知道文件名),还有很多要优化. 譬如:不能看可以下载的文件,输入错误无法处理,不能处理多个用户,每次只能下载一个结束,服务器没有完成守护进程:没有用函数封装,简化m ...