Coder Social home page Coder Social logo

abc-arbitrage / disruptor-cpp Goto Github PK

View Code? Open in Web Editor NEW
339.0 55.0 100.0 1.3 MB

Port of LMAX Disruptor to C++

License: Apache License 2.0

CMake 0.90% C++ 86.88% C 0.53% Batchfile 0.01% Makefile 0.57% M4 0.56% Python 9.96% Shell 0.59%
cpp lmax-disruptor

disruptor-cpp's Introduction

Disruptor-cpp

Build Status Build status

Overview

Disruptor-cpp is a fully functional C++ port of the LMAX disruptor. Implements all the features available in java Disruptor v3.3.7.

Building

Compilers

  • Clang 3.8 or newer
  • GCC 5.0 or newer
  • Microsoft Visual C++ 2015 or newer

Linux

Boost must be available on your machine. You can install it using your favorite package manager or build it on your own. If Boost has been installed into standard system locations the following commands will start the build:

mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=release
make

If Boost has been installed into a custom location you will probably need to specify BOOST_ROOT variable. Please refer to the Find boost documentation for details.

Optionally you may want to compile and run the unit tests and benchmarks. The tests compilation is activated by means of DISRUPTOR_BUILD_TESTS flag:

cmake .. -DCMAKE_BUILD_TYPE=release -DDISRUPTOR_BUILD_TESTS=true

Windows

The simplest way to compile the library on Windows is to use the provided Visual Studio solution files:

  • Disruptor-lib.sln - the solution only includes the library.
  • Disruptor-all.sln - the solution includes the library, benchmarks and unit tests.

Boost must be available on your machine. The 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.

Getting started

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.

disruptor-cpp's People

Contributors

anokl avatar oktal avatar proffan avatar slumber avatar stiopaa1 avatar ybainier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

disruptor-cpp's Issues

Error: unknown mnemonic `rep' -- `rep' on arrch64

hello !

this is very nice and useful project thank you for making it available.

Sadly it doesn't build on aarch64 architecture (mac M1) as it replies on some assembly instructions which are not supported.

e.g

$ make
Scanning dependencies of target DisruptorStatic
[ 2%] Building CXX object Disruptor/CMakeFiles/DisruptorStatic.dir/BasicExecutor.cpp.o
[ 4%] Building CXX object Disruptor/CMakeFiles/DisruptorStatic.dir/BlockingWaitStrategy.cpp.o
[ 6%] Building CXX object Disruptor/CMakeFiles/DisruptorStatic.dir/BusySpinWaitStrategy.cpp.o
[ 9%] Building CXX object Disruptor/CMakeFiles/DisruptorStatic.dir/FixedSequenceGroup.cpp.o
[ 11%] Building CXX object Disruptor/CMakeFiles/DisruptorStatic.dir/ProcessingSequenceBarrier.cpp.o
[ 13%] Building CXX object Disruptor/CMakeFiles/DisruptorStatic.dir/ProducerType.cpp.o
[ 15%] Building CXX object Disruptor/CMakeFiles/DisruptorStatic.dir/RoundRobinThreadAffinedTaskScheduler.cpp.o
[ 18%] Building CXX object Disruptor/CMakeFiles/DisruptorStatic.dir/Sequence.cpp.o
[ 20%] Building CXX object Disruptor/CMakeFiles/DisruptorStatic.dir/SequenceGroups.cpp.o
[ 22%] Building CXX object Disruptor/CMakeFiles/DisruptorStatic.dir/SleepingWaitStrategy.cpp.o
[ 25%] Building CXX object Disruptor/CMakeFiles/DisruptorStatic.dir/SpinWait.cpp.o
/tmp/ccVC3BXq.s: Assembler messages:
/tmp/ccVC3BXq.s:119: Error: unknown mnemonic rep' -- rep'
/tmp/ccVC3BXq.s:220: Error: unknown mnemonic rep' -- rep'
/tmp/ccVC3BXq.s:241: Error: unknown mnemonic rep' -- rep'
make[2]: *** [Disruptor/CMakeFiles/DisruptorStatic.dir/build.make:193: Disruptor/CMakeFiles/DisruptorStatic.dir/SpinWait.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:152: Disruptor/CMakeFiles/DisruptorStatic.dir/all] Error 2
make: *** [Makefile:163: all] Error 2

I don't expect you to fix this as you obviously made this implementation for your use case with low latency in mind - though may be you could still consider adding a similar fix as in this similar case?

nats-io/nats.c#87

Regards, Roman

clean way to install it with cmake

Hi everyone,
First of all, thank you very much for this library,
however i'm a bit lost on this one, does anyone know what is the cleanest way to install this library with cmake?

Is it lock-free?

How it can be lock-free when you use condition_variable and unique_lock?

Undefined Behaviour: Integer overflow on Sequence increment

Disruptor/EventPoller.h:57:37: runtime error: signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long

Java guarantees deterministic overflow behavior for signed integers but C++ doesn't resulting in undefined behaviour in various locations throughout the code - the above is only one example.

Used in production?

Thanks for making this available. Is this used in production, or intended to be "production ready"?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.