https://github.com/mattkretz/lockfree_ring
lock-free queue, implemented as a ring-buffer on the stack
https://github.com/mattkretz/lockfree_ring
cpp cpp17 lock-free ringbuffer
Last synced: 3 months ago
JSON representation
lock-free queue, implemented as a ring-buffer on the stack
- Host: GitHub
- URL: https://github.com/mattkretz/lockfree_ring
- Owner: mattkretz
- License: bsd-3-clause
- Created: 2017-03-01T11:22:39.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-24T08:09:56.000Z (about 9 years ago)
- Last Synced: 2025-04-07T02:35:22.342Z (about 1 year ago)
- Topics: cpp, cpp17, lock-free, ringbuffer
- Language: C++
- Homepage:
- Size: 32.2 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Lock-free queue using a ring buffer on the stack
==================================================
[](https://github.com/mattkretz/lockfree_ring/blob/master/LICENSE)
[](https://isocpp.org/)
[](https://github.com/mattkretz/virtest)
[](https://travis-ci.org/mattkretz/lockfree_ring)
[](https://ci.appveyor.com/project/mattkretz/lockfree_ring)
Example
--------------
```C++
#include
// consumer threads:
void consumer(vir::lockfree_ring &q)
{
while (keep_running) {
auto popper = q.pop_front();
for (;;) {
std::optional data = popper.get();
if (data) {
do_work(data.value);
break;
}
do_something_else();
}
}
}
// producer threads:
void producer(vir::lockfree_ring &q)
{
while (keep_running) {
Data data = produce_data();
auto pusher = q.prepare_push(std::move(data));
while (!pusher.try_push()) {
do_something_else();
}
}
}
```
Remarks
--------------
There is little guarantee about ordering and no guarantee about fairness.
However, if the size of the ring is larger than the number of active reader
objects (returned from `pop_front()`) then all consumers will eventually be
able to read a value (unless one of the writers does not fill its `pusher`
object).