Defined in <stlab/concurrency/channel.hpp>
template <typename E, typename...R>
auto zip(E e, R... upstream_receiver)
(1)
  1. This function creates receiver of type tuple<T...> where T... are the result_types of the passed upstream_receiver. Whenever a complete set of values from each upstream receiver has arrived, it passes the tuple with the values downstream.

Parameters

e

Executor which is used to schedule the resulting task

upstream_receiver

The upstream receiver(s).

Return value

a tuple<T...> where T... are the result_types of all upstream_receiver.

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> send1;
    receiver<int> receive1;
    sender<double> send2;
    receiver<double> receive2;
    tie(send1, receive1) = channel<int>(default_executor);
    tie(send2, receive2) = channel<double>(default_executor);

    std::atomic_int all_done{ 0 };

    auto zipped = zip(default_executor,
                      receive1, 
                      receive2)
        | [&_all_done = all_done](std::tuple<int, double> v) { 
              cout << get<0>(v) << " " << get<1>(v) << '\n';
              ++_all_done; 
          };

    receive1.set_ready();
    receive2.set_ready();

    // even the values are send in mixed order into the channels, the are passed
    // in step order into the zipped process
    send1(1);
    send2(2.5);
    send2(5.7);
    send1(4);

    while (all_done < 2) {
        this_thread::sleep_for(std::chrono::milliseconds(1));
    }
}

/*
    Result:
        1 2.5
        4 5.7
*/