0
修改数组元素我有个树节点结构为:原子采用Intel TBB
struct node
{
unsigned long key;
tbb::atomic<struct node*> lChild;
tbb::atomic<struct node*> rChild;
};
我将在lChild
和rChild
做compare_and_swap
。 我想让左侧和右侧的孩子作为数组元素,并且仍然能够对单个数组元素进行CAS操作。
注:我不打算做一个双CAS
我尝试这样做:
struct node
{
unsigned long key;
tbb::atomic<struct node*> childrenArray[2];
};
这:
struct node
{
unsigned long key;
tbb::atomic<struct node**> childrenArray;
};
但这里的各个数组元素是不是原子。如何修改此结构,以便我可以执行CAS操作:
node->childrenArray[0].compare_and_swap(newNode,oldNode);
其实你是对的。我对这种用法持怀疑态度。但正如你所说的那样,它编译并运行良好。 – arunmoezhi