stlab::channel
Creates a pair that consists of a sender and a receiver
Parameters
e |
Executor which is used to send 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 in case that T
is not of type void
. In case of type void
it returns only a channel of type void
.
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
*/