template <typename T, typename E>
std::pair<sender<T>, receiver<T>> channel(E e)
(1)
  1. This function creates a channel that consists of a sending and a receiving part of the channel.

Parameters

e

Executor which is used to sends the passed values from the sender down to the receiver.

Return value

Returns a pair of sender - receiver of type T that form a channel

Example 1

#include <atomic>
#include <iostream>
#include <thread>

#include <stlab/concurrency/channel.hpp>
#include <stlab/concurrency/default_executor.hpp>

using namespace std;
using namespace stlab;

int main() {
    sender<int> send;
    receiver<int> receive;

    tie(send, receive) = channel<int>(default_executor);

    std::atomic_bool done{ false };

    auto hold = receive | [&_done = done](int x) {  
        cout << x << '\n'; 
        _done = true; 
      };

    // It is necessary to mark the receiver side as ready, when all connections are
    // established
    receive.set_ready();

    send(1);
    send(2);
    send(3);

    // Waiting just for illustrational purpose
    while (!done.load()) {
        this_thread::sleep_for(chrono::milliseconds(1));
    }
}


/*
        Result:
            1
            2
            3
*/