All Projects → rigtorp → Mpmcqueue

rigtorp / Mpmcqueue

Licence: mit
A bounded multi-producer multi-consumer concurrent queue written in C++11

Programming Languages

cpp
1120 projects
cpp11
221 projects

Projects that are alternatives of or similar to Mpmcqueue

linked-blocking-multi-queue
A concurrent collection that extends the existing Java concurrent collection library, offering an optionally-bounded blocking "multi-queue" based on linked nodes.
Stars: ✭ 41 (-91.24%)
Mutual labels:  queue, concurrency
Goconcurrentqueue
Go concurrent-safe, goroutine-safe, thread-safe queue
Stars: ✭ 127 (-72.86%)
Mutual labels:  concurrency, queue
Arq
Fast job queuing and RPC in python with asyncio and redis.
Stars: ✭ 695 (+48.5%)
Mutual labels:  concurrency, queue
psched
Priority-based Task Scheduling for Modern C++
Stars: ✭ 59 (-87.39%)
Mutual labels:  queue, concurrency
easy-promise-queue
An easy JavaScript Promise queue which is automatically executed, concurrency controlled and suspendable.
Stars: ✭ 31 (-93.38%)
Mutual labels:  queue, concurrency
beems
a bee-queue based minimalist toolkit for building fast, decentralized, scalable and fault tolerant microservices
Stars: ✭ 33 (-92.95%)
Mutual labels:  queue, concurrency
Unagi Chan
A haskell library implementing fast and scalable concurrent queues for x86, with a Chan-like API
Stars: ✭ 115 (-75.43%)
Mutual labels:  concurrency, queue
think-async
🌿 Exploring cooperative concurrency primitives in Python
Stars: ✭ 178 (-61.97%)
Mutual labels:  queue, concurrency
queueable
Convert streams to async ⌛ iterables ➰
Stars: ✭ 43 (-90.81%)
Mutual labels:  queue, concurrency
Spscqueue
A bounded single-producer single-consumer wait-free and lock-free queue written in C++11
Stars: ✭ 307 (-34.4%)
Mutual labels:  concurrency, queue
Akka.net
Port of Akka actors for .NET
Stars: ✭ 4,024 (+759.83%)
Mutual labels:  concurrency
Post Me
📩 Use web Workers and other Windows through a simple Promise API
Stars: ✭ 398 (-14.96%)
Mutual labels:  concurrency
Go Wrk
go-wrk - a HTTP benchmarking tool based in spirit on the excellent wrk tool (https://github.com/wg/wrk)
Stars: ✭ 421 (-10.04%)
Mutual labels:  concurrency
Fetch
Simple & Efficient data access for Scala and Scala.js
Stars: ✭ 453 (-3.21%)
Mutual labels:  concurrency
Mangos
mangos is a pure Golang implementation of nanomsg's "Scalablilty Protocols"
Stars: ✭ 384 (-17.95%)
Mutual labels:  queue
Throat
Throttle a collection of promise returning functions
Stars: ✭ 419 (-10.47%)
Mutual labels:  concurrency
Nsq
A realtime distributed messaging platform
Stars: ✭ 20,663 (+4315.17%)
Mutual labels:  queue
Ava
Node.js test runner that lets you develop with confidence 🚀
Stars: ✭ 19,458 (+4057.69%)
Mutual labels:  concurrency
Atomic queue
C++ lockless queue.
Stars: ✭ 373 (-20.3%)
Mutual labels:  queue
Quasar
Fibers, Channels and Actors for the JVM
Stars: ✭ 4,349 (+829.27%)
Mutual labels:  concurrency

MPMCQueue.h

Build Status C/C++ CI License

A bounded multi-producer multi-consumer concurrent queue written in C++11.

It's battle hardened and used daily in production:

It's been cited by the following papers:

  • Peizhao Ou and Brian Demsky. 2018. Towards understanding the costs of avoiding out-of-thin-air results. Proc. ACM Program. Lang. 2, OOPSLA, Article 136 (October 2018), 29 pages. DOI: https://doi.org/10.1145/3276506

Example

MPMCQueue<int> q(10);
auto t1 = std::thread([&] {
  int v;
  q.pop(v);
  std::cout << "t1 " << v << "\n";
});
auto t2 = std::thread([&] {
  int v;
  q.pop(v);
  std::cout << "t2 " << v << "\n";
});
q.push(1);
q.push(2);
t1.join();
t2.join();

Usage

  • MPMCQueue<T>(size_t capacity);

    Constructs a new MPMCQueue holding items of type T with capacity capacity.

  • void emplace(Args &&... args);

    Enqueue an item using inplace construction. Blocks if queue is full.

  • bool try_emplace(Args &&... args);

    Try to enqueue an item using inplace construction. Returns true on success and false if queue is full.

  • void push(const T &v);

    Enqueue an item using copy construction. Blocks if queue is full.

  • template <typename P> void push(P &&v);

    Enqueue an item using move construction. Participates in overload resolution only if std::is_nothrow_constructible<T, P&&>::value == true. Blocks if queue is full.

  • bool try_push(const T &v);

    Try to enqueue an item using copy construction. Returns true on success and false if queue is full.

  • template <typename P> bool try_push(P &&v);

    Try to enqueue an item using move construction. Participates in overload resolution only if std::is_nothrow_constructible<T, P&&>::value == true. Returns true on success and false if queue is full.

  • void pop(T &v);

    Dequeue an item by copying or moving the item into v. Blocks if queue is empty.

  • bool try_pop(T &v);

    Try to dequeue an item by copying or moving the item into v. Return true on sucess and false if the queue is empty.

All operations except construction and destruction are thread safe.

Implementation

Memory layout

Enqeue:

  1. Acquire next write ticket from head.
  2. Wait for our turn (2 * (ticket / capacity)) to write slot (ticket % capacity).
  3. Set turn = turn + 1 to inform the readers we are done writing.

Dequeue:

  1. Acquire next read ticket from tail.
  2. Wait for our turn (2 * (ticket / capacity) + 1) to read slot (ticket % capacity).
  3. Set turn = turn + 1 to inform the writers we are done reading.

References:

Testing

Testing concurrency algorithms is hard. I'm using two approaches to test the implementation:

  • A single threaded test that the functionality works as intended, including that the element constructor and destructor is invoked correctly.
  • A multithreaded fuzz test that all elements are enqueued and dequeued correctly under heavy contention.

TODO

  • [X] Add allocator supports so that the queue could be used with huge pages and shared memory
  • [ ] Add benchmarks and compare to boost::lockfree::queue and others
  • [ ] Use C++20 concepts instead of static_assert if available
  • [X] Use std::hardware_destructive_interference_size if available
  • [ ] Add API for zero-copy deqeue and batch dequeue operations

About

This project was created by Erik Rigtorp <[email protected]>.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].