Giter Club home page Giter Club logo

disruptor4cpp's Introduction

disruptor4cpp

C++ port of LMAX disruptor.

I try to implement it as closely as possible to the Java version, with C++ features in mind.

The library requires C++11 features. Currently, it has been tested in GCC 4.8.

What's new?

2015-09-01: The core features except DSL from Java version 3.3.2 have been ported.

Getting Started

The library is header-only. Clone and copy the "include" folder. For example,

$ git clone https://github.com/alexleemanfui/disruptor4cpp.git
$ cd disruptor4cpp
$ mkdir /opt/disruptor4cpp/
$ cp -pr include/ /opt/disruptor4cpp/

To run the test,

$ git clone https://github.com/alexleemanfui/disruptor4cpp.git
$ cd disruptor4cpp
$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./disruptor4cpp_test

To use it, include the below header file

#include <disruptor4cpp/disruptor4cpp.h>

Example

#include <cstdint>
#include <exception>
#include <iostream>
#include <thread>

#include <disruptor4cpp/disruptor4cpp.h>

class int_handler : public disruptor4cpp::event_handler<int>
{
public:
	int_handler() = default;
	virtual ~int_handler() = default;
	virtual void on_start() { }
	virtual void on_shutdown() { }
	virtual void on_event(int& event, int64_t sequence, bool end_of_batch)
	{
		std::cout << "Received integer: " << event << std::endl;
	}
	virtual void on_timeout(int64_t sequence) { }
	virtual void on_event_exception(const std::exception& ex, int64_t sequence, int* event) { }
	virtual void on_start_exception(const std::exception& ex) { }
	virtual void on_shutdown_exception(const std::exception& ex) { }
};

int main(int argc, char* argv[])
{
	using namespace disruptor4cpp;

	// Create the ring buffer.
	ring_buffer<int, 1024, busy_spin_wait_strategy, producer_type::multi> ring_buffer;

	// Create and run the consumer on another thread.
	auto barrier = ring_buffer.new_barrier();
	int_handler handler;
	batch_event_processor<decltype(ring_buffer)> processor(ring_buffer, std::move(barrier), handler);
	std::thread processor_thread([&processor] { processor.run(); });
	
	// Publish some integers.
	for (int i = 0; i < 1000; i++)
	{
		int64_t seq = ring_buffer.next();
		ring_buffer[seq] = i;
		ring_buffer.publish(seq);
	}

	// Stop the consumer.
	std::this_thread::sleep_for(std::chrono::seconds(1));
	processor.halt();
	processor_thread.join();
	return 0;
}

disruptor4cpp's People

Contributors

alexleemanfui avatar

Stargazers

 avatar

Watchers

 avatar

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.