A complete hazard pointer implementation in modern C++ (C++17), with modular design, heavy comment and clean structure.
Here is a simple demo with incomplete code. For a complete program, refers to this file
template <typename T>
class stack : private cxxhazard::enable_hazard_from_this {
public:
struct node {
node *_next;
value_type *_data;
};
public:
// constructor/destructor/operator(s)...
public:
// push/size/empty/etc...
bool pop(T &dest)
{
auto old_head = _head.load();
auto hazard = make_hazard();
do {
old_head = hazard.protect(_head);
if (old_head == nullptr)
return false;
} while (!_head.compare_exchange_strong(old_head, old_head->_next));
hazard.unprotect();
dest = *old_head->_data;
retire(old_head, [old_head](){
delete old_head->_data;
delete old_head;
});
return true;
}
bool peek(T &dest)
{
auto hazard = make_hazard();
auto old_head = hazard.protect(_head);
if (old_head) {
dest = *old_head->_data;
return true;
}
return false;
}
private:
std::atomic<node *> _head;
};
File | Purpose |
---|---|
hazard.hpp | global header |
fwd.hpp | forward declarations |
resource.hpp | internal hazard_ptr data storage/manager |
ptr.hpp | hazard_ptr interface |
reclaim.hpp | retired node storage |
domain.hpp | necessary data storage/base class |
- Add more test unit, more test
- Provide extra example
- Better query data structure for resource_pool