-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreeStack.cpp
More file actions
40 lines (32 loc) · 992 Bytes
/
Copy pathFreeStack.cpp
File metadata and controls
40 lines (32 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <atomic>
#include <memory>
template <typename T>
class FreeStack {
public:
FreeStack() : head(nullptr) {}
~FreeStack() {
while (pop()); // Pop all elements to prevent memory leak
}
void push(const T& data) {
Node* new_node = new Node(data);
new_node->next = head.load();
while (!head.compare_exchange_strong(new_node->next, new_node));
}
std::shared_ptr<T> pop() {
Node* old_head = head.load();
while (old_head &&
!head.compare_exchange_strong(old_head, old_head->next));
return old_head ? old_head->data : std::shared_ptr<T>();
}
std::shared_ptr<T> peek() const {
Node* top_node = head.load();
return top_node ? top_node->data : std::shared_ptr<T>();
}
private:
struct Node {
std::shared_ptr<T> data;
Node* next;
Node(const T& data) : data(std::make_shared<T>(data)), next(nullptr) {}
};
std::atomic<Node*> head;
};