summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/examples/cpp14/executors/async_1.cpp
blob: 0d1bdfcb8bdfa6653ee9c5fa11141285f58eb292 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <asio/associated_executor.hpp>
#include <asio/bind_executor.hpp>
#include <asio/execution.hpp>
#include <asio/static_thread_pool.hpp>
#include <iostream>
#include <string>

using asio::bind_executor;
using asio::get_associated_executor;
using asio::static_thread_pool;
namespace execution = asio::execution;

// A function to asynchronously read a single line from an input stream.
template <class IoExecutor, class Handler>
void async_getline(IoExecutor io_ex, std::istream& is, Handler handler)
{
  // Track work for the handler's associated executor.
  auto work_ex = asio::prefer(
      get_associated_executor(handler, io_ex),
      execution::outstanding_work.tracked);

  // Post a function object to do the work asynchronously.
  asio::require(io_ex, execution::blocking.never).execute(
      [&is, work_ex, handler=std::move(handler)]() mutable
      {
        std::string line;
        std::getline(is, line);

        // Pass the result to the handler, via the associated executor.
        asio::prefer(work_ex, execution::blocking.possibly).execute(
            [line=std::move(line), handler=std::move(handler)]() mutable
            {
              handler(std::move(line));
            });
      });
}

int main()
{
  static_thread_pool io_pool(1);
  static_thread_pool completion_pool(1);

  std::cout << "Enter a line: ";

  async_getline(io_pool.executor(), std::cin,
      bind_executor(completion_pool.executor(),
        [](std::string line)
        {
          std::cout << "Line: " << line << "\n";
        }));

  io_pool.wait();
  completion_pool.wait();
}
?s=13&d=retro' /> braintro1-0/+3 2018-10-27Added color overlay to Yosaku To Donbei MASHinfo2-2/+89 2018-10-26new Non-Working machines - S Plus Set chips braintro2-1/+25 New Nonworking Machines ------------------------------------------------- S-Plus SET005 Set chip [BrianT] S-Plus SET015 Set chip [BrianT] S-Plus SET026 Set chip [BrianT] 2018-10-26new working machine - PE+ Set chip 033 braintro2-2/+16 New Working Machine ------------------------------------------------ Player's Edge Plus (SET033) Set Chip [BrianT] 2018-10-26m6801: Use devcb for ports; remove I/O space and MCFG_ macros (nw) AJR42-512/+281 2018-10-26abc1600mac: removed MCFG macro (nw) Ivan Vangelista3-24/+11 2018-10-26new not working clone Ivan Vangelista2-0/+11 ----------------------------------------- Heavy Metal Meltdown (German) [PinMAME] 2018-10-26m68k chk2cmp2: more consistent sign check (nw) hap1-9/+9 2018-10-26mpu401: Eliminate MCFG_ macros (nw) AJR3-18/+10 2018-10-26mcs96: Save the int_pending register as well (but note that it is not ↵ AJR1-1/+1 actually cleared upon reset) (nw) 2018-10-26mcs96: Add save state and miscellaneous other stuff (nw) AJR4-14/+68 2018-10-26bbcmc: Added expansion port and Mertec Companion expansion device. Nigel Barnes8-11/+598 2018-10-26bbcb: Added ReCo6502 co-processor. Nigel Barnes5-9/+400 2018-10-26electron_cass.xml: Corrections (nw) Nigel Barnes1-14/+14