Is this hazard pointer example erroneous due to an ABA problem?

In the C ++ Concurrency book in action, the author gave an example of using a hazard pointer to implement the data structure of a lock stack. Part of the code looks like this:

std::shared_ptr<T> pop()
{
    std::atomic<void*>& hp=get_hazard_pointer_for_current_thread();
    node* old_head=head.load();
    node* temp;
    do
    {
        temp=old_head;
        hp.store(old_head);
        old_head=head.load();
    } while(old_head!=temp);
    // ...
}

The description says that

while, , node hasnt head . , node. , head node , head , , , head , .

, , head node

+6
1

memory_order load() std::memory_order_seq_cst, ( )

memory_order_seq_cst B, M, :

  • A, M, B
  • , A, B M, memory_order_seq_cst, - A
  • , A, B M, memory_order_seq_cst.

, node (), , , , . , , .

, std::memory_order_seq_cst. , . , "" (old_head==temp) .

- , - , , .


, , . pop() , . , , /, , ( , ). . :

concurrent_stack<int> p;
if (!p.empty() && (p.top() == 5))
{
  auto t = p.pop();
  assert( t ); // May fail
  assert( *t == 5 ); // May fail
}

, , , , . pop(), , , .

+5

All Articles