项目作者: mattkretz

项目描述 :
lock-free queue, implemented as a ring-buffer on the stack
高级语言: C++
项目地址: git://github.com/mattkretz/lockfree_ring.git
创建时间: 2017-03-01T11:22:39Z
项目社区:https://github.com/mattkretz/lockfree_ring

开源协议:BSD 3-Clause "New" or "Revised" License

下载


Lock-free queue using a ring buffer on the stack

license
language
depends

Build Status
Build status

Example

  1. #include <lockfree_ring.h>
  2. // consumer threads:
  3. void consumer(vir::lockfree_ring<Data> &q)
  4. {
  5. while (keep_running) {
  6. auto popper = q.pop_front();
  7. for (;;) {
  8. std::optional<Data> data = popper.get();
  9. if (data) {
  10. do_work(data.value);
  11. break;
  12. }
  13. do_something_else();
  14. }
  15. }
  16. }
  17. // producer threads:
  18. void producer(vir::lockfree_ring<Data> &q)
  19. {
  20. while (keep_running) {
  21. Data data = produce_data();
  22. auto pusher = q.prepare_push(std::move(data));
  23. while (!pusher.try_push()) {
  24. do_something_else();
  25. }
  26. }
  27. }

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).