| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Disruptor-cpp is a fully functional C++ port of the LMAX disruptor. Implements all the features available in java Disruptor v3.3.7.
The library requires a C++20 compiler and has no third-party dependencies — only the standard library and the platform threading library (pthreads on Linux/macOS).
No external libraries are required. The following commands will start the build:
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=release
makeOptionally you may want to compile and run the unit tests and benchmarks. The tests are built with GoogleTest (bundled in googletest-release-1.8.0/) and compilation is activated by the DISRUPTOR_BUILD_TESTS flag:
cmake .. -DCMAKE_BUILD_TYPE=release -DDISRUPTOR_BUILD_TESTS=true
make
ctest # run the unit testsThe simplest way to compile the library on Windows is to use the provided Visual Studio solution files:
No external libraries are required; the project only needs a C++20-capable MSVC toolset.
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 will carry a single long value:
struct LongEvent
{
long value;
};The consumer will print the event value to standard output and also notify the publishing thread when everything is processed:
struct PrintingEventHandler : Disruptor::IEventHandler< LongEvent >
{
explicit PrintingEventHandler(int toProcess) : m_actuallyProcessed(0), m_toProcess(toProcess)
{}
void onEvent(LongEvent& event, int64_t, bool) override
{
std::cout << "Event: " << event.value << std::endl;
if (++m_actuallyProcessed == m_toProcess)
m_allDone.notify_all();
}
void waitEndOfProcessing()
{
std::unique_lock<decltype(m_mutex)> lk(m_mutex);
m_allDone.wait(lk);
}
private:
std::mutex m_mutex;
std::condition_variable m_allDone;
int m_toProcess;
int m_actuallyProcessed;
};Now we can wire all the things together:
auto const ExpectedNumberOfEvents = 10000;
auto const RingBufferSize = 1024;
// Instantiate and start the disruptor
auto eventFactory = []() { return LongEvent(); };
auto taskScheduler = std::make_shared< Disruptor::ThreadPerTaskScheduler >();
auto disruptor = std::make_shared< Disruptor::disruptor<LongEvent> >(eventFactory, RingBufferSize, taskScheduler);
auto printingEventHandler = std::make_shared< PrintingEventHandler >(ExpectedNumberOfEvents);
disruptor->handleEventsWith(printingEventHandler);
taskScheduler->start();
disruptor->start();
// Publish events
auto ringBuffer = disruptor->ringBuffer();
for (auto i = 0; i<ExpectedNumberOfEvents; ++i)
{
auto nextSequence = ringBuffer->next();
(*ringBuffer)[nextSequence].value = i;
ringBuffer->publish(nextSequence);
}
// Wait for the end of execution and shutdown
printingEventHandler->waitEndOfProcessing();
disruptor->shutdown();
taskScheduler->stop();For more details, please refer the original Java Disruptor documentation.
| Back | FazBrowse Home | New Git URL |