一起学JUCE之Atomic

  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即可

时间: 2024-07-30 04:10:04

一起学JUCE之Atomic的相关文章

一起学JUCE之HashMap

基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同.)此类不保证映射的顺序,特别是它不保证该顺序恒久不变. 此实现假定哈希函数将元素适当地分布在各桶之间,可为基本操作(get 和 put)提供稳定的性能.迭代 collection 视图所需的时间与 HashMap 实例的“容量”(桶的数量)及其大小(键-值映射关系数)成比例.所以,如果迭代性能很重要,

【转】程序员怎样学数学

I've been working for the past 15 months on repairing my rusty math skills, ever since I read a biography of Johnny von Neumann. I've read a huge stack of math books, and I have an even bigger stack of unread math books. And it's starting to come tog

CSDN日报20170407 ——《嘿,程序猿,你该学点经济学了!》

[程序人生]嘿,程序猿,你该学点经济学了! 作者:夏雨 笔者一直认为,一个好的程序猿,不仅仅是代码敲得好,其他方面的知识和能力同样很重要.特别是随着年龄的增长,很多人也慢慢的往管理层发展.这个时候沟通与协调能力变得更加重要,而一些策划,推广方面的知识也同样是不可缺少的. 说到这里我们不得不提到经济学.懂得一些经济学的知识,不仅能在工作中运用,还有一个大家很关心的作用,那就是理财! [Java 编程]死磕Java并发--深入分析CAS 作者:chenssy CAS,Compare And Swap

[转] 程序员怎样学数学

Source:http://article.yeeyan.org/view/pluto/2365 --------------------------------------------------------------------- 读后感: 高中的时候数学成绩还不错,150分的卷子基本能保持在135以上.但是总感觉我的数学思维和数学修养仍然没什么提高.NUAA自招失败的经历让我彻底发现了这一点.大一学了一年的高数,又被繁杂的公式折磨得死去活来. 总感觉真正的数学不应该是这样的.但是真正的数

juce中的引用计数

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

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

在前面的几篇文章,分析的都是Juce库里面Core模块的内存部分,除了骨灰级C++爱好者之外,貌似大家对这些都不是非常感兴趣.相信大家更想知道Juce是怎么用于产品开发,而对于它的构成不是非常感兴趣.天天写一些内存.指针.线程之类的文章.Skilla也厌倦了.这次来分析一下Juce的上层应用程序框架. 以下上一段Demo里的代码片段 class JuceDemoApplication : public JUCEApplication { public: JuceDemoApplication()

juce中的内存泄漏检测

非常值得借鉴的做法,基于引用计数和局部静态变量,代码比较简单不加详解. //============================================================================== /** Embedding an instance of this class inside another class can be used as a low-overhead way of detecting leaked instances. This cl

猫猫学iOS(四十九)多线程网络之线程的创建NSThreand

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 一:NSThread的基本使用 1:创建和启动线程 一个NSThread对象就代表一条线程 创建.启动线程 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(sel) object:nil]; [thread start];

还在用Synchronized?Atomic你了解不?

前言 只有光头才能变强 之前已经写过多线程相关的文章了,有兴趣的同学可以去了解一下: https://github.com/ZhongFuCheng3y/3y/blob/master/src/thread.md 在阅读<阿里巴巴 Java开发手册>读后感时,还有未解决的问题: 如果是count++操作,使用如下类实现: AtomicInteger count = new AtomicInteger(); count.addAndGet(1);如果是 JDK8,推荐使用 LongAdder 对象