There is probably a simple solution to this that I'm missing, but I'm out of ideas.
Here's the push function:
void pushFront(T t)
{
std::shared_ptr<Node<T> > newNode(new Node<T>(t));
std::shared_ptr<Node<T> > origHead, next = std::atomic_load(&_head);
do
{
if(std::atomic_load(&_head) == nullptr)
{
newNode->next = origHead;
next = newNode;
}
else
{
newNode->next = origHead;
next->aba = origHead->aba + 1;
next = newNode;
}
}
while(!atomic_compare_exchange_weak(&_head, &origHead, next));
}
And the structure of the node:
struct Node
{
T t;
uintptr_t aba;
std::shared_ptr<Node> next;
Node(const T& t) : t(t), aba(0), next(nullptr){}
};
As was stated, the first call to push appears to work. A new node is created, and it points to _head, which is has a value of nullptr. However, when it is called again, it encounters a segmentation fault.
From what I can tell with gdb, the failure seems to occur when it evaluates the expression in while(). Furthermore, the first call to push will generate a newNode at say address 0x1234. The second call will also generate a newNode at address 0x1234. My guess, then, is that the fault is a result of trying to modify already allocated memory. If this is the case, I must be lacking some insight as to what shared_ptr is really doing.
Any suggestions would be appreciated.
Aucun commentaire:
Enregistrer un commentaire