Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/curve/channel

๐Ÿงต A C++ implementation of Rust's std::sync::mpsc::channel
https://github.com/curve/channel

cpp cpp17 cpp17-library cpp20 cpp20-library mpsc mpsc-channel threading

Last synced: 3 months ago
JSON representation

๐Ÿงต A C++ implementation of Rust's std::sync::mpsc::channel

Awesome Lists containing this project

README

        

# ๐Ÿงต channel

A C++20 library that provides a rust like `std::sync::mpsc::channel` for inter-thread communication.

# ๐Ÿ“ฆ Installation

> [!NOTE]
> This library requires a C++20 capable compiler.
> In case you need support for C++17 checkout commits prior to [6977815](https://github.com/Curve/channel/tree/6977815409b4c3c02d74a7aee3fc29f01d632feb)

- FetchContent
```cmake
include(FetchContent)
FetchContent_Declare(channel GIT_REPOSITORY "https://github.com/Curve/channel")

FetchContent_MakeAvailable(channel)
target_link_libraries( cr::channel)
```
- Git Submodule
```bash
git submodule add "https://github.com/Curve/channel"
```
```cmake
# Somewhere in your CMakeLists.txt
add_subdirectory("")
target_link_libraries( cr::channel)
```

# ๐Ÿ“’ Examples

```cpp
#include
#include

#include

auto [sender, receiver] = cr::channel();

// Spawn off expensive computations
std::thread t1([sender = std::move(sender)]() mutable
{
sender.send(expensive_computation_that_returns_int());
sender.send(another_expensive_computation_that_returns_float());
});

// Do some useful work for awhile

// Let's see what that first answer was
std::cout << receiver.recv_as() << std::endl;

// Let's see what that second answer was
std::cout << receiver.recv_as() << std::endl;
```

> **Note**
> This library also supports methods like `try_recv` and `recv_timeout`.

> For more examples see [tests](tests/)