Created
September 29, 2019 22:48
-
-
Save tshev/cf64dcf46dac3da47fe8b40ee3155be6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Looks like almost good abstraction, but the consumer is very slow. I need to read the code of stlab.
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
g++ src/test_channels.cpp -I libs/libraries/ -std=c++17 -lboost_system -pthread && ./a.out