Sha256: 0b3fdd3cacf5ab7f4cc5055420543626496d91bfb7f10bb53b0e5f6d6b0dcb2a
Contents?: true
Size: 1.14 KB
Versions: 16
Compression:
Stored size: 1.14 KB
Contents
#include <asio/ts/executor.hpp> #include <asio/thread_pool.hpp> #include <iostream> #include <string> using asio::bind_executor; using asio::dispatch; using asio::make_work_guard; using asio::post; using asio::thread_pool; // A function to asynchronously read a single line from an input stream. template <class Handler> void async_getline(std::istream& is, Handler handler) { // Create executor_work for the handler's associated executor. auto work = make_work_guard(handler); // Post a function object to do the work asynchronously. post([&is, work, handler=std::move(handler)]() mutable { std::string line; std::getline(is, line); // Pass the result to the handler, via the associated executor. dispatch(work.get_executor(), [line=std::move(line), handler=std::move(handler)]() mutable { handler(std::move(line)); }); }); } int main() { thread_pool pool; std::cout << "Enter a line: "; async_getline(std::cin, bind_executor(pool, [](std::string line) { std::cout << "Line: " << line << "\n"; })); pool.join(); }
Version data entries
16 entries across 16 versions & 1 rubygems