Reputation: 737
I am working on a parallel avl tree and have ran into a problem. Here is the function that causes this problem:
template<typename T, int Threads>
bool PTreeSet<T, Threads>::tryInsert(Node* parent, Node* node) {
if (parent->key > node->key) {
return parent->left.compare_exchange_strong(nullptr, node); // <-- Error
} else if (parent->key < node->key) {
return parent->right.compare_exchange_strong(nullptr, node); // <-- Error
} else {
return false;
}
return true;
}
parent->left
has type atomic<Node*>
and I want to set that pointer to node
if the current value is null. The compiler complains with error
error: no matching member function for call to 'compare_exchange_strong'
return parent->left.compare_exchange_strong(nullptr, node);
Why is this not valid code?
Upvotes: 1
Views: 3369
Reputation: 52471
The first parameter of atomic<T>::compare_exhange_strong
is a T&
. It requires an lvalue. That's one half of the "exchange": the current value of the atomic is loaded into the object referred to by the first parameter.
You need something like this:
Node* tmp = nullptr;
parent->left.compare_exchange_strong(tmp, node);
As a side effect, if parent->left
is not in fact NULL
, you get its current value in tmp (which you can ignore if you don't need it, of course).
Upvotes: 4