All Projects → MengRao → SPMC_Queue

MengRao / SPMC_Queue

Licence: MIT License
A simple and efficient single producer multiple consumer queue, suititable for both ITC and IPC.

Programming Languages

C++
36643 projects - #6 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to SPMC Queue

Jocket
Low-latency java socket implementation (using shared memory)
Stars: ✭ 83 (+336.84%)
Mutual labels:  ipc, low-latency
Mappedbus
Mappedbus is a low latency message bus for Java microservices utilizing shared memory. http://mappedbus.io
Stars: ✭ 613 (+3126.32%)
Mutual labels:  ipc, low-latency
Scalecube Services
ScaleCube Services is a high throughput, low latency reactive microservices library built to scale. it features: API-Gateways, service-discovery, service-load-balancing, the architecture supports plug-and-play service communication modules and features. built to provide performance and low-latency real-time stream-processing. its open and designed to accommodate changes. (no sidecar in a form of broker or any kind)
Stars: ✭ 482 (+2436.84%)
Mutual labels:  ipc, low-latency
reactor-aeron
A reactive driver for Aeron transport (https://github.com/real-logic/aeron)
Stars: ✭ 43 (+126.32%)
Mutual labels:  ipc, low-latency
node-svmq
Native System V message queues in Node.js
Stars: ✭ 16 (-15.79%)
Mutual labels:  ipc
eth-provider
A Universal Ethereum Provider Client
Stars: ✭ 139 (+631.58%)
Mutual labels:  ipc
coreipc
WCF-like service model API for communication over named pipes and TCP. .NET and node.js clients.
Stars: ✭ 22 (+15.79%)
Mutual labels:  ipc
CSharpServer
Ultra fast and low latency asynchronous socket server & client C# library with support TCP, SSL, UDP protocols and 10K connections problem solution
Stars: ✭ 101 (+431.58%)
Mutual labels:  low-latency
pollnet
A collection of non-blocking(polling) network libs for Linux, also support solarflare APIs(Tcpdirect/Efvi)
Stars: ✭ 98 (+415.79%)
Mutual labels:  low-latency
InterProcessCommunication
Inter-process Communication
Stars: ✭ 11 (-42.11%)
Mutual labels:  ipc
ModernOperatingSystems AndrewTanenbaum
My notes after reading 'Modern Operating Systems' book by Andrew Tanenbaum and Herbert Bos.
Stars: ✭ 71 (+273.68%)
Mutual labels:  ipc
qtalk-go
versatile stream IO and RPC based IPC stack for Go
Stars: ✭ 29 (+52.63%)
Mutual labels:  ipc
Werk
High-throughput / low-latency C++ application framework
Stars: ✭ 30 (+57.89%)
Mutual labels:  low-latency
ipc-toolkit
A set of reusable functions to integrate IPC into an existing simulation.
Stars: ✭ 84 (+342.11%)
Mutual labels:  ipc
IpcEventBus
Faster than Intents and easier than AIDLs.
Stars: ✭ 35 (+84.21%)
Mutual labels:  ipc
cachegrand
cachegrand is an open-source fast, scalable and secure Key-Value store, also fully compatible with Redis protocol, designed from the ground up to take advantage of modern hardware vertical scalability, able to provide better performance and a larger cache at lower cost, without losing focus on distributed systems.
Stars: ✭ 87 (+357.89%)
Mutual labels:  low-latency
ripple
Simple shared surface streaming application
Stars: ✭ 17 (-10.53%)
Mutual labels:  low-latency
IPC.Bond
IPC.Bond is an extension of IPC library that provides inter-process communication using shared memory on Windows with Bond serialization.
Stars: ✭ 26 (+36.84%)
Mutual labels:  ipc
transport-stream-online-segmenter
Transport stream web based HLS segmenter.
Stars: ✭ 30 (+57.89%)
Mutual labels:  low-latency
metacom
RPC communication protocol for Metarhia stack 🔌
Stars: ✭ 42 (+121.05%)
Mutual labels:  ipc

SPMCQueue

SPMCQueue is actually a single publisher(writer) multiple subscriber(reader) queue C++ template class, which can be easily used for inter-thread or inter-process communication.

A key feature of this queue is that the writer won't wait if one of the readers is far behind, in which case we noramlly call a queue being "full". Actually there is no concept of being "full" in SPMC_Queue, the writer just keep writing without blocking or failure, it's not even aware of the existence of the readers. It's the reader's responsibility to not fall behind too much, if a reader is slower by more than queue size it'll start dropping messages. This is very simalar to udp multicasting in the network, where the sender doesn't care the receivers who subscribed the group and just send packages in its own speed, if a receiver is slow the NIC will drop packages when its incoming buffer is full. Likeyly, SPMC_Queue is "multicasting" within a host among threads or processes, in a pretty efficent way.

Usage

A SPMCQueue class is defined as SPMCQueue<T, CNT> where T is user msg type and CNT is the queue size; In the following example the queue is defined as

SPMCQueue<int, 1024> q;

The writer use a lambda callback function to write directly into the queue memory, making the write operation "zero-copy" and always successful:

q.write([](int& msg){
  msg = 123;
});

For readers, initially one reader needs to get a Reader object from the queue:

  auto reader = q.getReader();

The read operation is non-blocking, which means user needs to repetitively call the read()/readLast() function to poll new messages from the writer. read()/readLast() returns T* if it succeeds or nullptr if no new message:

  int* msg = reader.read();
  if (msg) {
    std::cout << "msg: "<< *msg << std::endl;
  }

The read/readLast operation is also zero-copy: it simply returns the address of the next or the last object the writer has written, but the user should not save the reference for too long as it could be rewritten by the writer, it's better to copy the object for user himself if its content needs to be saved.

Examples

There are example codes in example dir for both ITC and IPC usage.

Performance

Don't bother, there is no more efficient implementation in the world.

Use example/multhread.cc for benchmark, on a host with "Intel(R) Xeon(R) Gold 6144 CPU @ 3.50GHz" and cpu isolated and pinned, in one reader scenario, average latency is 335 tsc(95.7 ns), and one additional reader makes the latency 40 tsc(11.4 ns) higher.

Multiple Producer Version?

Actually SPMCQueue can be easily altered to supoort multiple producer by replacing ++write_idx with atomic fetch add. But multiple producer queue is a bad design because producers' contention deteriorates performance(in this case producers contend on write_idx). It's better to use multiple SPMCQueue, each for one producer thread, and have consumers poll on all these queues.

An Implementation for Dynamic-Sized Msg

If you're looking for a SPMC queue which can handle messages with dynamic size(like a real multicasting udp package), check PubSubQueue.

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