I have a tree node structure as:
struct node
{
unsigned long key;
tbb::atomic<struct node*> lChild;
tbb::atomic<struct node*> rChild;
};
I would be doing compare_and_swap
on lChild
and rChild
. I want make the left and right child as array elements and still be able to do CAS on individual array elements.
Note: I do not intend to do a double CAS
I tried this:
struct node
{
unsigned long key;
tbb::atomic<struct node*> childrenArray[2];
};
and this:
struct node
{
unsigned long key;
tbb::atomic<struct node**> childrenArray;
};
but here the individual array elements are not atomic. How do I modify this structure so that I can do a CAS like:
node->childrenArray[0].compare_and_swap(newNode,oldNode);
Why the first approach doesn't work for you? Unless you intend to perform atomic update on the whole array, it is the way to go. I compiled the following code successfully:
struct node
{
unsigned long key;
tbb::atomic<struct node*> childrenArray[2];
};
void main() {
node n;
n.childrenArray[0].compare_and_swap(0,0);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.