Skip to content

Commit a0f4b39

Browse files
committed
Add getting started section
1 parent 797ef87 commit a0f4b39

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
## Overview
88

99
Disruptor-cpp is a fully functional C++ port of the [LMAX disruptor](https://lmax-exchange.github.io/disruptor/).
10+
Implements all the features available in java Disruptor v3.3.7.
1011

1112
## Building
1213

@@ -43,3 +44,79 @@ The simplest way to compile the library on Windows is to use the provided Visual
4344

4445
[Boost](http://www.boost.org/) must be available on your machine. The [**boost.props**](https://github.com/Abc-Arbitrage/Disruptor-cpp/blob/master/boost.props) file already included into the solution but you may need to modify the headers and libraries directory according to your boost location and folder structure.
4546

47+
### Getting started
48+
49+
To give you a taste of C++ disruptor let us consider a very basic example where the event is passed from producer to consumer. The event carries a single long value:
50+
51+
```Cpp
52+
struct LongEvent
53+
{
54+
long value;
55+
};
56+
```
57+
58+
The consumer will print the event value to standard output and also notify the publishing thread when everything is processed:
59+
60+
```Cpp
61+
struct PrintingEventHandler : Disruptor::IEventHandler< LongEvent >
62+
{
63+
explicit PrintingEventHandler(int toProcess) : m_actuallyProcessed(0), m_toProcess(toProcess)
64+
{}
65+
66+
void onEvent(LongEvent& event, int64_t, bool) override
67+
{
68+
std::cout << "Event: " << event.value << std::endl;
69+
70+
if (++m_actuallyProcessed == m_toProcess)
71+
allDone.notify_all();
72+
}
73+
74+
void waitEndOfProcessing()
75+
{
76+
std::unique_lock<std::mutex> lk(m);
77+
allDone.wait(lk);
78+
}
79+
80+
private:
81+
std::mutex m;
82+
std::condition_variable allDone;
83+
int m_toProcess;
84+
int m_actuallyProcessed;
85+
};
86+
```
87+
88+
Now we can wire all the things together:
89+
90+
```Cpp
91+
auto const ExpectedNumberOfEvents = 10000;
92+
auto const RingBufferSize = 1024;
93+
94+
// Instantiate and start the disruptor
95+
auto eventFactory = []() { return LongEvent(); };
96+
auto taskScheduler = std::make_shared< Disruptor::ThreadPerTaskScheduler >();
97+
98+
auto disruptor = std::make_shared< Disruptor::disruptor<LongEvent> >(eventFactory, RingBufferSize, taskScheduler);
99+
auto printingEventHandler = std::make_shared< PrintingEventHandler >(ExpectedNumberOfEvents);
100+
101+
disruptor->handleEventsWith(printingEventHandler);
102+
103+
taskScheduler->start();
104+
disruptor->start();
105+
106+
// Publish events
107+
auto ringBuffer = disruptor->ringBuffer();
108+
for (auto i = 0; i<ExpectedNumberOfEvents; ++i)
109+
{
110+
auto nextSequence = ringBuffer->next();
111+
(*ringBuffer)[nextSequence].value = i;
112+
ringBuffer->publish(nextSequence);
113+
}
114+
115+
// Wait for the end of execution and shutdown
116+
printingEventHandler->waitEndOfProcessing();
117+
118+
disruptor->shutdown();
119+
taskScheduler->stop();
120+
```
121+
122+
For more details, please refer the original [Java Disruptor documentation](https://lmax-exchange.github.io/disruptor).

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy