Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andreiavrammsd/cpp-channel
Thread-safe container for sharing data between threads
https://github.com/andreiavrammsd/cpp-channel
channel concurrent-queue cpp multithreading queue synchronized-queue thread-safe thread-safe-queue
Last synced: 5 days ago
JSON representation
Thread-safe container for sharing data between threads
- Host: GitHub
- URL: https://github.com/andreiavrammsd/cpp-channel
- Owner: andreiavrammsd
- License: mit
- Created: 2020-02-11T16:20:01.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-08-11T13:21:06.000Z (3 months ago)
- Last Synced: 2024-08-11T16:49:41.505Z (3 months ago)
- Topics: channel, concurrent-queue, cpp, multithreading, queue, synchronized-queue, thread-safe, thread-safe-queue
- Language: C++
- Homepage: https://blog.andreiavram.ro/cpp-channel-thread-safe-container-share-data-threads/
- Size: 129 KB
- Stars: 330
- Watchers: 9
- Forks: 28
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Channel
[![build](https://github.com/andreiavrammsd/cpp-channel/workflows/build/badge.svg)](https://github.com/andreiavrammsd/cpp-channel/actions)
[![codecov](https://codecov.io/github/andreiavrammsd/cpp-channel/graph/badge.svg?token=CKQ0TVW62Z)](https://codecov.io/github/andreiavrammsd/cpp-channel)
### Thread-safe container for sharing data between threads. Header-only.
* Thread-safe push and fetch.
* Use stream operators to push (<<) and fetch (>>) items.
* Value type must be default constructible.
* Blocking (forever waiting to fetch).
* Range-based for loop supported.
* Close to prevent pushing and stop waiting to fetch.
* Integrates well with STL algorithms in some cases. Eg: std::move(ch.begin(), ch.end(), ...).
* Tested with GCC, Clang, and MSVC.## Requirements
* C++11 or newer
## Installation
Copy the [include](./include) directory to your project and add it to your include path. Or
see [CMakeLists.txt](./examples/cmake-project/CMakeLists.txt) from the [CMake project example](./examples/cmake-project)
.## Usage
```c++
#include#include
int main() {
msd::channel chan; // unbufferedint in = 1;
int out = 0;// Send to channel
chan << in;// Read from channel
chan >> out;assert(out == 1);
}
``````c++
#includeint main() {
msd::channel chan{2}; // buffered// Send to channel
chan << 1;
chan << 2;
chan << 3; // blocking because capacity is 2 (and no one reads from channel)
}
``````c++
#includeint main() {
msd::channel chan{2}; // bufferedint in = 1;
int out = 0;// Send to channel
chan << in;
chan << in;// Read from channel
chan >> out;
chan >> out;
chan >> out; // blocking because channel is empty (and no one writes on it)
}
``````c++
#include#include
int main() {
msd::channel chan;int in1 = 1;
int in2 = 2;chan << in1 << in2;
for (const auto out : chan) { // blocking: forever waiting for channel items
std::cout << out << '\n';
}
}
```See [examples](examples).
Developed with [CLion](https://www.jetbrains.com/?from=serializer)