From ff01b716711b97c2fcaa709ea97f7650f106aa10 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Fri, 7 Oct 2016 14:13:19 +0200 Subject: Added ASIO networking library (nw) --- .../asio/src/examples/cpp14/executors/async_1.cpp | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 3rdparty/asio/src/examples/cpp14/executors/async_1.cpp (limited to '3rdparty/asio/src/examples/cpp14/executors/async_1.cpp') diff --git a/3rdparty/asio/src/examples/cpp14/executors/async_1.cpp b/3rdparty/asio/src/examples/cpp14/executors/async_1.cpp new file mode 100644 index 00000000000..d282b74f206 --- /dev/null +++ b/3rdparty/asio/src/examples/cpp14/executors/async_1.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +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 +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(); +} -- cgit v1.2.3-70-g09d2