https://github.com/bhhbazinga/lockfreequeue
Lock Free Queue Based On Hazard Pointer.
https://github.com/bhhbazinga/lockfreequeue
cplusplus hazard-pointers lockfreequeue threads
Last synced: about 2 months ago
JSON representation
Lock Free Queue Based On Hazard Pointer.
- Host: GitHub
- URL: https://github.com/bhhbazinga/lockfreequeue
- Owner: bhhbazinga
- Created: 2019-11-17T15:58:47.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-27T03:30:16.000Z (over 5 years ago)
- Last Synced: 2025-04-17T22:09:01.358Z (about 2 months ago)
- Topics: cplusplus, hazard-pointers, lockfreequeue, threads
- Language: C++
- Homepage:
- Size: 65.4 KB
- Stars: 37
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LockFreeQueue
Lock Free Queue Based On Hazard Pointer.
## Feature
* Thread-safe and Lock-free.
* Hazard pointer.
* ABA safe.
* Inter thread helping.
* Singly-linked list with a sentinel node.
* Support Multi-producer & Multi-consumer.
* Dynamic size.
* Dynamically allocate nodes(performance bottlneck). Or you can simply implement a thread-safe memory pool, e.g. by thread_local storage identifier.
## BenchmarkMagnitude | Enqueue | Dequeue | Enqueue & Dequeue|
:----------- | :-----------| :-----------| :-----------------
10K | 1.8ms | 1.4ms | 2.6ms
100K | 33.6ms | 32.3ms | 38.6ms
1000K | 209.2ms | 185.1ms | 299.3ms
The above data was tested on my i5-7500 cpu with gcc -O3.\
You can also compare the tested data with [BlockingQueue](https://github.com/bhhbazinga/BlockingQueue) which is implemented by mutex.The data of first and second column was obtained by starting 4 threads to enqueue concurrently and dequeue concurrently, the data of third column was obtained by starting 2 threads to enqueue and 2 threads to dequeue concurrently, each looped 10 times to calculate the average time consumption.
See also [test](test.cc).
## Build
```
make && ./test
```
## API
```C++
void Enqueue(const T& data);
void Enqueue(T&& data);
void Emplace(Args&&... args);
bool Dequeue(T& data);
size_t size() const;
```
## Reference
[Lock-Free Data Structures with Hazard Pointers](https://www.drdobbs.com/lock-free-data-structures-with-hazard-po/184401890)\
[C++ Concurrency in Action, Second Edition](https://chenxiaowei.gitbook.io/c-concurrency-in-action-second-edition-2019/)