Skip to content

Instantly share code, notes, and snippets.

@tshev
Created September 29, 2019 22:48
Show Gist options
  • Save tshev/cf64dcf46dac3da47fe8b40ee3155be6 to your computer and use it in GitHub Desktop.
Save tshev/cf64dcf46dac3da47fe8b40ee3155be6 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stlab/concurrency/channel.hpp>
#include <stlab/concurrency/concurrency.hpp>
#include <stlab/concurrency/serial_queue.hpp>
struct object_t {
int a, b, c;
object_t() = default;
object_t(const object_t&) = default;
};
int M = 100000;
int main() {
stlab::sender<object_t> sender;
stlab::receiver<object_t> receiver;
std::tie(sender, receiver) = stlab::channel<object_t>(stlab::default_executor);
size_t count_received = 0;
std::atomic<bool> done = false;
auto holder = receiver | [&](auto x) {
++count_received;
if (count_received % M == 0) {
std::cout << "Received " << count_received << std::endl;
}
};
receiver.set_ready();
for (size_t i = 0; i < 100000000ul;) {
if (++i % M == 0) {
std::cout << "Sent " << i << std::endl;
}
sender(object_t{});
}
std::cout << "WAIT\n";
while (!done.load()) {}
std::cout << count_received << std::endl;
}
@tshev
Copy link
Author

tshev commented Sep 29, 2019

g++ src/test_channels.cpp -I libs/libraries/ -std=c++17 -lboost_system -pthread && ./a.out

@tshev
Copy link
Author

tshev commented Sep 29, 2019

Looks like almost good abstraction, but the consumer is very slow. I need to read the code of stlab.

@tshev
Copy link
Author

tshev commented Sep 29, 2019

Maybe the code runs slowly, because I don't use libdispatch, but I am not sure. I need to know about the details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment