Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
LockFreeStack Class Reference
Collaboration diagram for LockFreeStack:
Collaboration graph

Public Member Functions

 LockFreeStack (hazard_pointer_manager &hp_mgr)
 
 ~LockFreeStack ()
 
void push (int value)
 
bool pop (int &result)
 

Private Attributes

std::atomic< TestNode * > head_ {nullptr}
 
hazard_pointer_manager & hp_manager_
 

Detailed Description

Examples
hazard_pointer_sample.cpp.

Definition at line 34 of file hazard_pointer_sample.cpp.

Constructor & Destructor Documentation

◆ LockFreeStack()

LockFreeStack::LockFreeStack ( hazard_pointer_manager & hp_mgr)
inlineexplicit
Examples
hazard_pointer_sample.cpp.

Definition at line 40 of file hazard_pointer_sample.cpp.

40: hp_manager_(hp_mgr) {}
hazard_pointer_manager & hp_manager_

◆ ~LockFreeStack()

LockFreeStack::~LockFreeStack ( )
inline
Examples
hazard_pointer_sample.cpp.

Definition at line 42 of file hazard_pointer_sample.cpp.

42 {
43 while (auto* node = head_.load()) {
44 head_.store(node->next.load());
45 delete node;
46 }
47 }
std::atomic< TestNode * > head_

References head_.

Member Function Documentation

◆ pop()

bool LockFreeStack::pop ( int & result)
inline
Examples
hazard_pointer_sample.cpp.

Definition at line 58 of file hazard_pointer_sample.cpp.

58 {
59 auto hp = hp_manager_.acquire();
60
61 while (true) {
62 auto* head = hp.protect(head_);
63 if (!head) {
64 return false; // Stack is empty
65 }
66
67 auto* next = head->next.load();
68
69 // Try to update head
70 if (head_.compare_exchange_weak(head, next)) {
71 result = head->data.load();
72 hp_manager_.retire(head);
73 return true;
74 }
75 }
76 }
A template class representing either a value or an error.

References head_, and hp_manager_.

Referenced by demonstrate_concurrent_access().

Here is the caller graph for this function:

◆ push()

void LockFreeStack::push ( int value)
inline
Examples
hazard_pointer_sample.cpp.

Definition at line 49 of file hazard_pointer_sample.cpp.

49 {
50 auto* new_node = new TestNode(value);
51 auto* old_head = head_.load();
52
53 do {
54 new_node->next.store(old_head);
55 } while (!head_.compare_exchange_weak(old_head, new_node));
56 }

References head_.

Referenced by demonstrate_concurrent_access().

Here is the caller graph for this function:

Member Data Documentation

◆ head_

std::atomic<TestNode*> LockFreeStack::head_ {nullptr}
private
Examples
hazard_pointer_sample.cpp.

Definition at line 36 of file hazard_pointer_sample.cpp.

36{nullptr};

Referenced by pop(), push(), and ~LockFreeStack().

◆ hp_manager_

hazard_pointer_manager& LockFreeStack::hp_manager_
private
Examples
hazard_pointer_sample.cpp.

Definition at line 37 of file hazard_pointer_sample.cpp.

Referenced by pop().


The documentation for this class was generated from the following file: