Skip to content

Commit 43392bb

Browse files
committed
Added README.md and LICENSE.txt in preparation for open-source release.
1 parent ced6121 commit 43392bb

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

Diff for: LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 Lewis Baker
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

Diff for: README.md

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
Disruptor++
2+
===========
3+
4+
Disruptor++ is a C++11 header-only implementation of the 'disruptor' data structure used
5+
to communicate between threads in a high-performance producer/consumer arrangement.
6+
7+
See the LMAX [technical paper](http://disruptor.googlecode.com/files/Disruptor-1.0.pdf)
8+
for a description of the theory behind the disruptor.
9+
10+
See the [Java LMAX Disruptor project](http://lmax-exchange.github.io/disruptor/) for
11+
more resources relating to the disruptor.
12+
13+
Description
14+
-----------
15+
16+
A disruptor data structure is essentially a ring buffer that uses different cursors
17+
to keep track of where consumers or producers have processed up to.
18+
19+
A disruptor can be used with either single producer thread or multiple producer
20+
threads. Multiple producer threads are able to publish items out-of-sequence
21+
so that producer threads do not hold up other producer threads unless the
22+
entire ring buffer fills up while waiting for a slow producer to publish its item.
23+
Consumers, however, always process items in the sequence they were enqueued.
24+
25+
The disruptor data structure supports batched operations with producers able to
26+
enqueue multiple items with a single synchronisation operation and consumers
27+
that fall behind are able to catch up by consuming batches of consecutive
28+
items with a single synchronisation operation.
29+
30+
When used with a single producer thread with the spin wait-strategy the disruptor
31+
uses only atomic reads/writes of integers and acquire/release memory barriers for
32+
synchronisation. It does not use CAS operations or locks that require kernel
33+
arbitration.
34+
35+
This implementation of the disruptor data-structure is defined fully in header files.
36+
There are no libraries to link against and many functions should be able to be inlined.
37+
Also, this implementation does not make use of abstract interfaces or virtual function
38+
calls, which can inhibit inlining and incur additional runtime overhead, but instead
39+
prefers to use templates for compile-time polymorphism.
40+
41+
Performance
42+
-----------
43+
44+
TODO: Put some performance results in here.
45+
46+
Synopsis
47+
--------
48+
49+
A single producer/single consumer use of a disruptor for communication.
50+
51+
```
52+
#include <disruptorplus/ring_buffer.hpp>
53+
#include <disruptorplus/single_threaded_claim_strategy.hpp>
54+
#include <disruptorplus/spin_wait_strategy.hpp>
55+
#include <disruptorplus/sequence_barrier.hpp>
56+
57+
#include <thread>
58+
59+
using namespace disruptorplus;
60+
61+
struct Event
62+
{
63+
uint32_t data;
64+
};
65+
66+
int main()
67+
{
68+
const size_t bufferSize = 1024; // Must be power-of-two
69+
70+
ring_buffer<Event> buffer(bufferSize);
71+
72+
spin_wait_strategy waitStrategy;
73+
single_threaded_claim_strategy<spin_wait_strategy> claimStrategy(bufferSize, waitStrategy);
74+
sequence_barrier<spin_wait_strategy> consumed(waitStrategy);
75+
claimStrategy.add_claim_barrier(consumed);
76+
77+
std::thread consumer([&]()
78+
{
79+
uint64_t sum = 0;
80+
sequence_t nextToRead = 0;
81+
bool done = false;
82+
while (!done)
83+
{
84+
// Wait until more items available
85+
sequence_t available = claimStrategy.wait_until_published(nextToRead);
86+
87+
// Process all available items in a batch
88+
do
89+
{
90+
auto& event = buffer[nextToRead];
91+
sum += event.data;
92+
if (event.data == 0)
93+
{
94+
done = true;
95+
}
96+
} while (nextToRead++ != available);
97+
98+
// Notify producer we've finished consuming some items
99+
consumed.publish(available);
100+
}
101+
std::cout << "sum is " << sum << std::endl;
102+
});
103+
104+
std::thread producer([&]()
105+
{
106+
for (uint32_t i = 1; i <= 1000000; ++i)
107+
{
108+
// Claim a slot in the ring buffer, waits if buffer is full
109+
sequence_t seq = claimStrategy.claim_one();
110+
111+
// Write to the slot in the ring buffer
112+
buffer[seq].data = i;
113+
114+
// Publish the event to the consumer
115+
claimStrategy.publish(seq);
116+
}
117+
118+
// Publish the terminating event.
119+
sequence_t seq = claimStrategy.claim_one():
120+
buffer[seq].data = 0;
121+
claimStrategy.publish(seq);
122+
});
123+
124+
consumer.join();
125+
producer.join();
126+
127+
return 0;
128+
}
129+
```
130+
131+
License
132+
-------
133+
134+
The Discruptor++ library is available under the MIT open source license.
135+
See LICENSE.txt for details.
136+
137+
Building
138+
--------
139+
140+
As this library is a header-only library there is no library component
141+
that needs to be built separately. Simply add the include/ directory
142+
to your include path and include the appropriate headers.
143+
144+
If you want to build the samples/tests then you can use the
145+
[Cake](https://github.com/lewissbaker/cake) build system to build
146+
this code.
147+
148+
Once you have installed cake you can simply run 'cake' in the root
149+
directory to build everything.

0 commit comments

Comments
 (0)