c++ - C++中 volatile 指针数组的声明

标签 c++ xcode atomic-swap

我有一个名为 Ambigous 的结构,在该结构中我有一个指向其他 Ambigous 的指针数组。

我想使用 OSAtomic.h 库来做 CompareandSwaps。

但是,我无法让阵列正常运行。

 OSAtomicCompareAndSwapPtrBarrier(void *__oldValue, void *__newValue, 
void *volatile *__theValue)

是比较和交换函数。

在我的结构中我有

 Ambigous * volatile* list;

电话是

bool res=OSAtomicCompareAndSwapPtrBarrier(current_node, new_node, local->list[pos]);

当我试图通过 cas 时

bool res=OSAtomicCompareAndSwapPtrBarrier(current_node, new_node, (void * volatile *)local->list[pos]);

我得到一个错误的 EXE_BAD_ACCESS

所以我想我要回答的是我应该如何声明 volatile 指针数组?

最佳答案

也许你想要

bool res = OSAtomicCompareAndSwapPtrBarrier(
              current_node, new_node, local->list + pos);

注意CAS操作基本上就是

bool CAS(void* old, void* new_, void* volatile* val) {
   /*atomic*/ {
     if (old == *val) {
         *val = new_;
         return true;
     } else
         return false;
   }
}

如果你传递一个list[pos],第三个参数将是Ambigous*类型,*val将是类型struct Ambigous 无法与指针进行比较。

关于c++ - C++中 volatile 指针数组的声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5396474/

相关文章:

linux - Win32 InterlockedIncrement 和 InterlockedExchange 是跨进程的原子吗?

c++ - 在 Xcode 中为 OpenCV 组合两个 c++ 文件(使用未声明的标识符 CVSquares)

c++ - 带星号和符号的函数参数

ios - MasterDetail 模板中的属性和 ivars

winapi - 以原子方式交换两个无符号 32 位变量 (ULONG) 的正确方法是什么?

mysql - 原子地更改 mySQL 中的多行

c++ - Word-Net线程安全

c++ - 如何修复用作初始值设定项错误的数组?

iphone - Xcode退出,iOS SDK中出现错误254

swift - Xcode 9 - 测试目标 X 遇到错误(无法连接到测试管理器)