@@ -7,6 +7,8 @@ you could use this queue completely from a single thread if you wish (but that w
77
88Note: If you need a general-purpose multi-producer, multi-consumer lock free queue, I have [ one of those too] [ mpmc ] .
99
10+ This repository also includes a [ circular-buffer SPSC queue] [ circular ] which supports blocking on enqueue as well as dequeue.
11+
1012
1113## Features
1214
@@ -25,7 +27,7 @@ Note: If you need a general-purpose multi-producer, multi-consumer lock free que
2527
2628## Use
2729
28- Simply drop the readerwriterqueue.h and atomicops.h files into your source code and include them :-)
30+ Simply drop the readerwriterqueue.h (or readerwritercircularbuffer.h) and atomicops.h files into your source code and include them :-)
2931A modern compiler is required (MSVC2010+, GCC 4.7+, ICC 13+, or any C++11 compliant compiler should work).
3032
3133Note: If you're using GCC, you really do need GCC 4.7 or above -- [ 4.6 has a bug] [ gcc46bug ] that prevents the atomic fence primitives
@@ -94,6 +96,25 @@ means care must be taken to only call `wait_dequeue` if you're sure another elem
9496will come along eventually, or if the queue has a static lifetime. This is because
9597destroying the queue while a thread is waiting on it will invoke undefined behaviour.
9698
99+ The blocking circular buffer has a fixed number of slots, but is otherwise quite similar to
100+ use:
101+
102+ ``` cpp
103+ BlockingReaderWriterCircularBuffer<int > q (1024); // pass initial capacity
104+
105+ q.try_enqueue(1);
106+ int number;
107+ q.try_dequeue(number);
108+ assert(number == 1);
109+
110+ q.wait_enqueue(123);
111+ q.wait_dequeue(number);
112+ assert(number == 123);
113+
114+ q.wait_dequeue_timed(number, std::chrono::milliseconds(10));
115+ ```
116+
117+
97118## CMake installation
98119As an alternative to including the source files in your project directly,
99120you can use CMake to install the library in your system's include directory:
@@ -118,14 +139,6 @@ includes all modern processors (e.g. x86/x86-64, ARM, and PowerPC). *Not* for us
118139Note that it's only been tested on x86(-64); if someone has access to other processors I'd love to run some tests on
119140anything that's not x86-based.
120141
121- Finally, I am not an expert. This is my first foray into lock-free programming, and though I'm confident in the code,
122- it's possible that there are bugs despite the effort I put into designing and testing this data structure.
123-
124- Use this code at your own risk; in particular, lock-free programming is a patent minefield, and this code may very
125- well violate a pending patent (I haven't looked). It's worth noting that I came up with this algorithm and
126- implementation from scratch, independent of any existing lock-free queues.
127-
128-
129142## More info
130143
131144See the [LICENSE.md][license] file for the license (simplified BSD).
@@ -139,3 +152,4 @@ about lock-free programming.
139152[benchmarks]: http://moodycamel.com/blog/2013/a-fast-lock-free-queue-for-c++#benchmarks
140153[gcc46bug]: http://stackoverflow.com/questions/16429669/stdatomic-thread-fence-has-undefined-reference
141154[mpmc]: https://github.com/cameron314/concurrentqueue
155+ [circular]: readerwritercircularbuffer.h
0 commit comments