MacOSX:OSAtomic与OSAtomicBarrier [英] MacOSX: OSAtomic vs OSAtomicBarrier

查看:128
本文介绍了MacOSX:OSAtomic与OSAtomicBarrier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处的功能:

#include <libkern/OSAtomic.h>

有OSAtomic和OSAtomicBarrier版本.

there are OSAtomic and OSAtomicBarrier versions.

但是,文档没有显示以下示例代码:

However, the documentation does not show sample code for:

  1. 什么时候可以安全地使用OSAtomic,而不使用OSAtomicBarrier版本
  2. 什么时候OSAtomic不安全,但是OSAtomicBarrier是安全的.
  1. When is it safe to use just OSAtomic, without the OSAtomicBarrier version
  2. When is it that OSAtomic would be unsafe, but OSAtomicBarrier would be safe.

任何人都可以提供说明和示例代码吗?

Can anyone provide explainations + sample codes?

[没有实际代码的您的意见"的随意散布是没有用的.读者:请对此类答案投下反对票;并用实际的代码强烈地回答问题.]

[Random ramblings of "your opinion" without actual code is useless. Readers: please down vote such answers; and vigrously upvote answers with actual code.]

[首选C/C ++代码;组装也可以.]

[C/C++ code preferred; Assembly okay too.]

推荐答案

在Intel和单处理器平台上,没关系.

On Intel and uniprocessor platforms, it doesn't matter.

对于多处理器PPC系统,除非原子存储不会影响除原子变量之外的任何数据,否则应始终使用函数的障碍种类.

For multiprocessor PPC systems, you should always use the barrier variety of functions, unless the atomic store affects no data other than the atomic variable.

以下内容将不可行:

data_structure[y].data++;
OSAtomicIncrement32(y);

必须在此处使用屏障,因为其他线程可能认为data_structure已过时.

You must use a barrier here, because other threads may see data_structure as out of date.

但是,如果出于某种目的将原子变量单独使用,则可以省略障碍:

However, if you are using an atomic variable for some purpose where it stands alone, you may omit the barrier:

// y is not used to access any other data
OSAtomicIncrement32(y);

精细,只要y的值不影响任何共享数据结构的变量即可.

Fine, as long as the value of y does not affect the variable of any shared data structure.

本质上,这是一个缓存刷新.您始终可以安全地使用屏障函数,但是在某些情况下,可以通过不使用屏障函数来提高性能,例如,如果相对于数据结构不使用y.在没有障碍的情况下使用功能的情况可能很少.

Essentially, it's a cache flush. You can always safely use the barrier functions, but in some cases, you may be able to improve performance by not using the barrier functions, such as if y is not used relative to a data structure. There are probably not many cases where you can use the functions without the barrier.

这篇关于MacOSX:OSAtomic与OSAtomicBarrier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆