Fast C++ single producer single consumer implementation

纵然是瞬间 提交于 2019-12-05 17:46:17

You will want to look at Intel's Thread Building Blocks. They build on the primitives provided by x86's user-mode atomic operations, and pthreads or Win32 threads, and provide fast, efficient, templated data structures. A concurrent queue is among many.

Just stumbled on this: CDS (Concurrent Data Structures) tonight.

CDS (Concurrent Data Structures) is a C++ template library of lock-free and fine-grained algorithms. It contains a collection of concurrent data structures: queues, maps, hazard pointer reclamation schema, - and many others

I must say I'm only slightly past the /getting it to build/ stage (it wasn't as straightforward as I'd have liked) but ... you might be interested to take a look for yourself.

The problem is that POSIX doesn't include an API (that I know of) for interlocked operations. In fact, not all platforms support the same operations for lock-free programming (some use compare-and-swap, some use load-linked-store-conditional).

That said, the only difficult part about making a lock-free single-consumer queue (supporting multiple producers is trivial) is handling the ABA problem, and that really shouldn't be a problem.

You need a singly-linked-list for producers to add to (prepend to). The consumer has a local queue, when that's exhausted it grabs the entire producer list and reverses it, creating a new local queue. The Windows SList API is an example.

Cameron

In addition to the other answers here (and in this highly related question), I'll take this opportunity for a shameless plug of my own super-fast, C++ implementation of a single-consumer single-producer wait-free queue. It:

  • Uses C++11 move semantics
  • Grows as needed (but only if you want it to)
  • Does lock-free memory management for the elements (using pre-allocated contiguous blocks)
  • Is stand-alone (two headers plus a license and readme)
  • Compiles under MSVC2010+, Intel ICC 13, and GCC 4.7.2 (and should work under any C++11 fully-compliant compiler)

It's available on GitHub under the simplified BSD license (feel free to fork it!).

A comparable queue is Facebook's Folly queue, which can be ever-so-slightly faster, but does not support growing as needed (it has a fixed size).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!