项目作者: p-id

项目描述 :
single producer single consumer bounded length queue
高级语言: C
项目地址: git://github.com/p-id/spscq.git
创建时间: 2018-05-09T12:06:46Z
项目社区:https://github.com/p-id/spscq

开源协议:MIT License

下载


LockfreeSPSCQueue

License

This is header only, C implementation of a lockfree single producer single consumer bounded queue for concurrent programming.

This implementation is best suited for heavy data movement between single producer and single consumer, typical application scenarios are video streaming pipeline and for sharing data across CPUs. The implementation ensures there are no additional memory copies while sharing the data between threads.

The code has been battle tested in production for quite sometime, but do report issues if observed.

Example

  1. #define ELEMENT_TYPE payload_t*
  2. #define SPSC_QUEUE_SIZE 8192
  3. spsc_queue_t queue;
  4. // producer thread
  5. payload_t* payload_in = NULL;
  6. payload_in = malloc(sizeof(payload_t)); // alternatively get payload blob from object-pool
  7. load_data(payload_in);
  8. enqueue(&queue, payload_in);
  9. // consumer thread
  10. payload_t* payload_out = NULL;
  11. dequeue(&queue, &payload_out);
  12. consume_func(payload_out);
  13. free(payload_out); // alternatively return payload_out back to object-pool

Only a single writer thread can perform enqueue operations and only a single reader thread can perform dequeue operations. Any other usage is invalid.

However, one can bundle the SPSC queue implementation with provided spin-lock implementation.
As a note of caution the current spin-lock implementation uses a lot of assembly code which might need a bit of porting effort [Ran in production handling ~100k requests per second on Intel x86_64 Dell hardware]

Implementation

The underlying implementation uses ring buffer.

Care has been taken to make sure to avoid any issues with false sharing.

The algorithm is optimized further to use an adaptive [B-Tree style] lookahead for queue head/tail indexes to avoid cache-line faults.

References:

See Also

  • Folly MPMC Queue Open source by Facebook
  • Elle C++14 asynchronous/coroutine framework used by Infiniti distributed-encrypted filesystem
  • SeaStar non-blocking asynchronous event-driven C++14 framework used by NOSQL ScyllaDB (see tutorial)
  • Boson C++14 asynchronous/coroutine framework similar to Go channels
  • MC FastFlow Multi-Core friendly framework in old C++98 (published in 2009) and still maintained

About

This project was created by Piyush Dewnani