Skip to content

Commit 6134dee

Browse files
authored
add early exit to .pop()
- seems odd to perform work on a node that could potentially be a nullptr from the moment the function is called (regardless of CAS) - same thing applies to `void pop(T &val)` (early exit allows the code the run without throwing for bad access to `old_head->next`)
1 parent 6e7ae1d commit 6134dee

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

listings/listing_7.3.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class lock_free_stack
2323
}
2424
std::shared_ptr<T> pop()
2525
{
26+
if (!head_) { return std::shared_ptr<T>(); }
2627
node* old_head=head.load();
27-
while(old_head &&
28-
!head.compare_exchange_weak(old_head,old_head->next));
29-
return old_head ? old_head->data : std::shared_ptr<T>();
28+
while(!head.compare_exchange_weak(old_head,old_head->next));
29+
return old_head->data;
3030
}
3131
};

0 commit comments

Comments
 (0)