Golang-style channels for C++
This was used internally by some Censys-related code. This is a dump of the code with some minor changes so that it's usable outside of Censys.
Easiest is to just copy both source files in the build system for your project. If this is popular, at some point, I might add a Makefile to this repository for testing purposes, but for now I'm just supplying the code as is.
Dummy example asynchronously processes a vector, and writes the output to a file.
dadrian::Channel<std::string> to_write;
std::fstream output_file("output.txt");
// Write output to a file as it comes through the queue
std::thread output_thread([&]() {
for (auto it = to_write.range(); it.valid(); ++it) {
if (it->size() > 0) {
output_file << *it;
}
}
output_file.flush();
});
// Read input and send through queue
std::vector<Object> input_data = get_input();
for (const auto& obj : input_data) {
// Do some work
std::string = do_something(obj);
// Sending releases ownership
to_write.send(std::move(s));
}
// Close the channel when completed, and join to the output thread until the
// channel is drained.
to_write.close();
output_thread.join();
I'll take pull requests, but I'm not going to provide much other support.