
Thread-safe container for sharing data between threads (synchronized queue). Header-only. Compatible with C++11 and newer.
About
msd::channel
- A synchronized queue that can be easily and safely shared between multiple threads.
- Tested with GCC, Clang, and MSVC.
- Uses std::mutex for synchronization.
- Uses a customizable storage to store elements.
It's a class that can be constructed in several ways:
- Buffered:
- The channel accepts a specified number of elements, after which it blocks the writer threads and waits for a reader thread to read an element.
- It blocks the reader threads when channel is empty until a writer thread writes elements.
- msd::channel<int> chan{2};
- Unbuffered:
- Never blocks writes.
- It blocks the reader threads when channel is empty until a writer thread writes elements.
- msd::channel<int> chan{};
- Heap- or stack-allocated: pass a custom storage or choose a built-in storage:
A storage is:
- A class with a specific interface for storing elements.
- Must implement FIFO logic.
- See built-in storages.
Exceptions:
Features
- Thread-safe push and fetch.
- Use stream operators to push (<<) and fetch (>>) items.
- Value type must be default constructible, move constructible, move assignable, and destructible.
- Blocking (forever waiting to fetch).
- Range-based for loop supported.
- Close to prevent pushing and stop waiting to fetch.
- Integrates with some of the STL algorithms. Eg:
- std::move(ch.begin(), ch.end(), ...)
- std::transform(input_chan.begin(), input_chan.end(), msd::back_inserter(output_chan)).
- std::copy_if(chan.begin(), chan.end(), ...);
Installation
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 .
Usage
int main()
{
chan << 1 << 2;
int first_value{};
int second_value{};
chan >> first_value >> second_value;
}
Thread-safe container for sharing data between threads.
Definition channel.hpp:87
bool read(T &out)
Pops an element from the channel.
Definition channel.hpp:190
int main()
{
chan << 1;
chan << 3;
}
bool write(Type &&value)
Pushes an element into the channel.
Definition channel.hpp:165
#include <iostream>
int main()
{
chan << 1 << 2;
for (int value : chan) {
if (chan.closed()) {
break;
}
std::cout << value << '\n';
}
}
#include <algorithm>
int main()
{
}
blocking_writer_iterator< Channel > back_inserter(Channel &chan)
Creates a blocking iterator for the given channel.
Definition blocking_iterator.hpp:211
iterator end() noexcept(std::is_nothrow_default_constructible< T >::value &&std::is_nothrow_move_assignable< T >::value)
Returns an iterator representing the end of the channel.
Definition channel.hpp:283
iterator begin() noexcept(std::is_nothrow_default_constructible< T >::value &&std::is_nothrow_move_assignable< T >::value)
Returns an iterator to the beginning of the channel.
Definition channel.hpp:272
void close() noexcept
Closes the channel, no longer accepting new elements.
Definition channel.hpp:234
NODISCARD size_type size() const noexcept
Returns the current size of the channel.
Definition channel.hpp:213
channel< T, array_storage< T, Capacity > > static_channel
Thread-safe container for sharing data between threads.
Definition static_channel.hpp:28
See examples and tests. Read the documentation for full API reference.
Known limitations
- In some cases, the integration with some STL algorithms does not compile with MSVC. See the Transform test.
Developed with CLion and Visual Studio Code.