A small C++17 hazard pointer implementation written as a learning project.
The goal of this project is to study how hazard pointers can be used for safe memory reclamation in lock-free data structures. It includes a basic hazard pointer interface, retired-object reclamation, and a simple stack example.
A simplified usage pattern:
bool pop(T& dest)
{
auto hazard = make_hazard();
node* old_head;
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;
}For a complete example, see test/test01.cpp.
| File | Purpose |
|---|---|
hazard.hpp |
Main public header |
ptr.hpp |
Hazard pointer interface |
resource.hpp |
Internal hazard pointer storage |
reclaim.hpp |
Retired object reclamation |
domain.hpp |
Domain/base class support |
fwd.hpp |
Forward declarations |
This project is mainly for studying lock-free memory reclamation. It is not intended to be a production-ready library.
- Add more tests
- Add more examples
- Improve the internal hazard pointer lookup structure