Coder Social home page Coder Social logo

sosflyyi / cpp-channel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andreiavrammsd/cpp-channel

0.0 0.0 0.0 122 KB

Thread-safe container for sharing data between threads

Home Page: https://blog.andreiavram.ro/cpp-channel-thread-safe-container-share-data-threads/

License: MIT License

C++ 58.07% CMake 41.93%

cpp-channel's Introduction

Channel

build

Thread-safe container for sharing data between threads. Header-only.

  • Thread-safe push and fetch.
  • Use stream operators to push (>>) and fetch (<<) items.
  • Blocking (forever waiting to fetch).
  • Range-based for loop supported.
  • Close to prevent pushing and stop waiting to fetch.
  • Integrates well with STL algorithms. Eg: std::move(ch.begin(), ch.end(), ...).
  • Tested with GCC and Clang.

Requirements

  • C++11 or newer

Installation

Copy the include directory to your project and add it to your include path. Or see CMakeLists.txt from the CMake project example .

Usage

#include <cassert>

#include <msd/channel.hpp>

int main() {
    msd::channel<int> chan; // unbuffered

    int in = 1;
    int out = 0;

    // Send to channel
    in >> chan;

    // Read from channel
    out << chan;

    assert(out == 1);
}
#include <msd/channel.hpp>

int main() {
    msd::channel<int> chan{2}; // buffered

    int in = 1;

    // Send to channel
    in >> chan;
    in >> chan;
    in >> chan; // blocking because capacity is 2 (and no one reads from channel)
}
#include <msd/channel.hpp>

int main() {
    msd::channel<int> chan{2}; // buffered

    int in = 1;
    int out = 0;

    // Send to channel
    in >> chan;
    in >> chan;

    // Read from channel
    out << chan;
    out << chan;
    out << chan; // blocking because channel is empty (and no one writes on it)
}
#include <iostream>

#include <msd/channel.hpp>

int main() {
    msd::channel<int> chan;

    int in1 = 1;
    in1 >> chan;

    int in2 = 2;
    in2 >> chan;

    for (const auto out : chan) {  // blocking: forever waiting for channel items
        std::cout << out << '\n';
    }
}

See examples.


Developed with CLion

JetBrains

cpp-channel's People

Contributors

andreiavrammsd avatar fwflunky avatar tenheadedlion 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.