Coder Social home page Coder Social logo

l3's Introduction

L3 - Low Latency Lib

C++ tools for building low latency systems. Includes a very fast and light implementation of an [LMAX disruptor] (https://lmax-exchange.github.io/disruptor). Code is written in c++11, makes use of std::atomic and therefore requires a suitable compiler. This is a header only library. Built and tested with Apple LLVM 5.1 on OS 10.10.3.

Simple example usage:

#include <L3/disruptor/disruptor.h>

#include <thread>
#include <iostream>

//
// Lets say we're going to use size_t as our message.
//
using Msg = size_t;
//
// Declare alias for a disruptor for this type of message and a ring
// buffer of size 2^19 = 512K messages.
//
using D = L3::Disruptor<Msg, 19>;
//
// Alias Put and Get types using defaults.
//
using Get = D::Get<>;
using Put = D::Put<>;
//
// We've now created a very basic disruptor with a single producer and
// consumer. We can use it like this...
//
int
main()
{
    //
    // To do a put we make an instance of the Put class we've just
    // defined.
    //
    {
        //
        // Creating a put claims a slot in the ring.
        //
        Put p;
        //
        // To write a value use assignment.
        //
        p = 1;
        //
        // When p goes out of scope the put is commited and published
        // to any consumers. It's important to note that once you have
        // started a put it will always be commited. RAII takes care
        // of that.
        //
    }
    //
    // Since unnamed temporaries have the lifetime of the statement
    // they are created in we can shorten the above to:
    //
    Put() = 2;
    //
    // To get we do something similar but gets are batched so we need
    // to read in a loop.
    //
    for(Msg m: Get())
    {
        std::cout << "got: " << m << std::endl;
    }
    //
    // We can send messages between two threads like so.
    //
    // 10 Million messages takes less than a second on my old macbook
    // pro.
    //
    const size_t loops = 10000000; 
    //
    // Producer just writes sequencial integers.
    //
    std::thread producer([]{ for(size_t i = 1; i < loops; ++i) Put() = i; });
    //
    // Consumer checks that these are read in the same order.
    //
    Msg previous = 0;
    //
    // Should get the same number of messages we put.
    //
    for(size_t i = 1; i < loops;)
    {
        for(Msg m: Get())
        {
            if(m != previous + 1)
            {
                //
                // Will abort since producer not joined.
                //
                return -1;
            }
            previous = m;
            ++i;
        }
    }
    producer.join();
    return 0;
}

See examples directory for how more complex topologies are supported.

l3's People

Contributors

abwilson avatar jkauffman1 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.