一起学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即可
一起学JUCE之Atomic的更多相关文章
- 一起学JUCE之HashMap
基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同.) ...
- juce中的内存泄漏检测
非常值得借鉴的做法,基于引用计数和局部静态变量,代码比较简单不加详解. //============================================================== ...
- juce中的引用计数
这个类提供了最基本的引用计数管理,界面库中,经常都需要消息发送,而带来的后果就是不知道消息中包含的对象是否还存在,如果不能很好管理的话就容易出现访问销毁了的对象这样的情况,所以,juce的界面无素也基 ...
- 还在用Synchronized?Atomic你了解不?
前言 只有光头才能变强 之前已经写过多线程相关的文章了,有兴趣的同学可以去了解一下: https://github.com/ZhongFuCheng3y/3y/blob/master/src/thre ...
- 图学java基础篇之并发
概述 并发处理本身就是编程开发重点之一,同时内容也很繁杂,从底层指令处理到上层应用开发都要涉及,也是最容易出问题的地方.这块知识也是评价一个开发人员水平的重要指标,本人自认为现在也只是学其皮毛,因此本 ...
- Juce源代码分析(九)应用程序基类ApplicationBase
在前面的几篇文章,分析的都是Juce库里面Core模块的内存部分,除了骨灰级C++爱好者之外,貌似大家对这些都不是非常感兴趣.相信大家更想知道Juce是怎么用于产品开发,而对于它的构成不是非常感兴趣. ...
- 跟着阿里p7一起学java高并发 - 第18天:玩转java线程池,这一篇就够了
java中的线程池,这一篇就够了 java高并发系列第18篇文章. 本文主要内容 什么是线程池 线程池实现原理 线程池中常见的各种队列 自定义线程创建的工厂 常见的饱和策略 自定义饱和策略 线程池中两 ...
- 跟着阿里p7一起学java高并发 - 第19天:JUC中的Executor框架详解1,全面掌握java并发核心技术
这是java高并发系列第19篇文章. 本文主要内容 介绍Executor框架相关内容 介绍Executor 介绍ExecutorService 介绍线程池ThreadPoolExecutor及案例 介 ...
- 学Python必背的初级单词,你都背了吗?
今天给大家分享一些学习Python必须认识的英文单词,同时也是学习编程都必须会的单词,新手赶快学起来!有点长耐心看完. 小编推荐一个学Python的学习裙:九三七六六七 五零九,无论你是大牛还是小白, ...
随机推荐
- GameUnity 2.0 文档(一) 事件机制
新版本和旧版本一样,有socket事件和内部事件.区别在于,你只要一个监听就可以 消息协议规则: 用类名标记协议的好处是什么? 是利用反射机制来处理每个消息. 程序启动的时候,会读取所有类名,并字典保 ...
- m,mm,mmm的用法
通过查看android源码目录下的build/envsetup.sh文件,可知: - m: Makes from the top of the tree. - mm: Build ...
- System services not available to Activities before onCreate()
应用中涉及到系统的mac地址获取,应该是不能够在oncreate()以前使用
- 为什么做Web开发要选择PHP
大部分互联网公司做WEb开发都选择PHP,PHP的优势在哪?你应该知道的 以前偶尔被人问到,为什么你(和大部分互联网公司)做Web开发要选择PHP, PHP有什么好处.简单的回答便是“PHP简单,开发 ...
- 当浏览器窗体改变时,div跟着变动方法
$(function(){ resizeU(); $(window).resize(resizeU); }); function resizeU() { var divkuangH = $(windo ...
- c# 执行js的方法
http://www.cnblogs.com/wuhuacong/archive/2010/11/08/1871866.html 为了有效阻止恶意用户的攻击,一般登录都会采用验证码方式方式处理登录,类 ...
- php源码分析之base64_encode函数
base64_encode编码规律分析 字符串长度除以3向上取整乘以4等于编码后的字符串长度 ceil(strlen($string)/3)*4 = strlen(base64_encode($str ...
- jstat undocumented
jstat -J-Djstat.showUnsupported=true -name btrace.com.sun.btrace.samples.ThreadCounter.count 11674 h ...
- 风格一致的backItem在项目中怎样设置
在相应的navigationController中重写- (void)pushViewController:(UIViewController *)viewController animated:(B ...
- 转:web_reg_save_param的使用详解
[摘要]利用实际案例说明如何使用Mercury LoadRunner提取包含在 HTML 页内的动态信息并创建参数. [关键词]性能测试,压力测试,Mercury LoadRunner 应用范围 在使 ...