Changeset 44665


Ignore:
Timestamp:
Apr 21, 2008, 1:36:28 AM (10 years ago)
Author:
chris_kohlhoff
Message:

Enhance example to make it clear that invocation hooking can be used with
asynchronous operations.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libs/asio/example/invocation/prioritised_handlers.cpp

    r43472 r44665  
    1313#include <iostream>
    1414#include <queue>
     15
     16using boost::asio::ip::tcp;
    1517
    1618class handler_priority_queue
     
    109111//----------------------------------------------------------------------
    110112
    111 void high_priority_handler()
     113void high_priority_handler(const boost::system::error_code& /*ec*/)
    112114{
    113115  std::cout << "High priority handler\n";
    114116}
    115117
    116 void middle_priority_handler()
     118void middle_priority_handler(const boost::system::error_code& /*ec*/)
    117119{
    118120  std::cout << "Middle priority handler\n";
     
    130132  handler_priority_queue pri_queue;
    131133
     134  // Post a completion handler to be run immediately.
    132135  io_service.post(pri_queue.wrap(0, low_priority_handler));
    133   io_service.post(pri_queue.wrap(100, high_priority_handler));
    134   io_service.post(pri_queue.wrap(42, middle_priority_handler));
     136
     137  // Start an asynchronous accept that will complete immediately.
     138  tcp::endpoint endpoint(boost::asio::ip::address_v4::loopback(), 0);
     139  tcp::acceptor acceptor(io_service, endpoint);
     140  tcp::socket server_socket(io_service);
     141  acceptor.async_accept(server_socket,
     142      pri_queue.wrap(100, high_priority_handler));
     143  tcp::socket client_socket(io_service);
     144  client_socket.connect(acceptor.local_endpoint());
     145
     146  // Set a deadline timer to expire immediately.
     147  boost::asio::deadline_timer timer(io_service);
     148  timer.expires_at(boost::posix_time::neg_infin);
     149  timer.async_wait(pri_queue.wrap(42, middle_priority_handler));
    135150
    136151  while (io_service.run_one())
Note: See TracChangeset for help on using the changeset viewer.