A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/andreiavrammsd/cpp-channel below:

andreiavrammsd/cpp-channel: C++11 thread-safe container for sharing data between threads (synchronized queue)

Thread-safe container for sharing data between threads (synchronized queue). Header-only. Compatible with C++11 and newer.

msd::channel

It's a class that can be constructed in several ways:

A storage is:

Exceptions:

Choose one of the methods:

VERSION=X.Y.Z \
    && wget https://github.com/andreiavrammsd/cpp-channel/archive/refs/tags/v$VERSION.zip \
    && unzip v$VERSION.zip \
    && cd cpp-channel-$VERSION \
    && mkdir build && cd build \
    && cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local \
    && sudo cmake --install .
// Unbuffered channel

#include <msd/channel.hpp>

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

    chan << 1 << 2;  // Send

    int first_value{};
    int second_value{};
    chan >> first_value >> second_value;  // Receive
    chan.read(first_value);  // Returns channel close status (true/false), blocks thread when channel is empty
}
// Buffered channel with custom storage

#include <msd/channel.hpp>

int main()
{
    msd::channel<int, msd::vector_storage<int>> chan{2};

    chan << 1;      // Throws if channel is closed
    chan.write(2);  // Non-throwing write, returns channel close status (true/false)
    chan << 3;      // Blocks thread (no space, no reader)
}
// Range-based iteration

#include <msd/channel.hpp>

#include <iostream>

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

    chan << 1 << 2;
    for (int value : chan) {
        if (chan.closed()) {
            // You can break before it's empty
            break;
        }

        std::cout << value << '\n';  // Blocks thread until there is data to read or channel is closed and empty
    }
}
// Channel with statically-allocated storage (always buffered)

#include <msd/static_channel.hpp>

#include <algorithm>

int main()
{
    msd::static_channel<int, 2> src{};
    msd::static_channel<int, 2> dst{};

    src.write(1);
    src.write(2);
    src.close();

    std::copy_if(src.begin(), src.end(), msd::back_inserter(dst), [](int value) { return value % 2 == 0; });

    dst.size();  // 1
}

See examples and tests. Read the documentation for full API reference.

Developed with CLion and Visual Studio Code.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4