lock_free_queue.h
C++_4th/Part_IV_Standard_Library/Advanced_Projects/lock_free_queue.h
#ifndef LOCK_FREE_QUEUE_H
#define LOCK_FREE_QUEUE_H
#include <atomic>
#include <memory>
// Lock-free queue implementation using atomic operations
template<typename T>
class LockFreeQueue {
private:
struct Node {
std::atomic<T*> data;
std::atomic<Node*> next;
Node() : data(nullptr), next(nullptr) {}
};
std::atomic<Node*> head_;
std::atomic<Node*> tail_;
public:
LockFreeQueue() {
Node* dummy = new Node;
head_.store(dummy);
tail_.store(dummy);
}
~LockFreeQueue() {
while (Node* const old_head = head_.load()) {
head_.store(old_head->next);
delete old_head;
}
}
// Enqueue an item
void enqueue(T item) {
Node* const new_node = new Node;
T* const data = new T(std::move(item));
Node* const prev_tail = tail_.exchange(new_node);
prev_tail->data.store(data);
prev_tail->next.store(new_node);
}
// Dequeue an item
bool dequeue(T& result) {
Node* head = head_.load();
Node* new_head = head->next.load();
if (new_head == nullptr) {
return false;
}
T* const data = new_head->data.load();
if (data == nullptr) {
return false;
}
result = *data;
delete data;
head_.store(new_head);
delete head;
return true;
}
// Check if queue is empty
bool empty() const {
Node* head = head_.load();
Node* tail = tail_.load();
return head == tail;
}
};
#endif // LOCK_FREE_QUEUE_H
Artigos relacionados
vector_example.cpp
vector_example.cpp — cpp source code from the C++ 4th learning materials (C++_4th/Capstone_Project/examples/vector_example.cpp).
Ler artigo →algorithm.h
algorithm.h — c source code from the C++ 4th learning materials (C++_4th/Capstone_Project/include/mini_stl/algorithm.h).
Ler artigo →map.h
map.h — c source code from the C++ 4th learning materials (C++_4th/Capstone_Project/include/mini_stl/map.h).
Ler artigo →thread_pool.h
thread_pool.h — c source code from the C++ 4th learning materials (C++_4th/Capstone_Project/include/mini_stl/thread_pool.h).
Ler artigo →vector.h
vector.h — c source code from the C++ 4th learning materials (C++_4th/Capstone_Project/include/mini_stl/vector.h).
Ler artigo →template_metaprogramming.cpp
template_metaprogramming.cpp — cpp source code from the C++ 4th learning materials (C++_4th/Examples/Advanced_Features/template_metaprogramming.cpp).
Ler artigo →