I've recently been playing around with boost asio and some new c++11 constructs. Here is a sample section of code that causes unexpected behavior (for me at least).
void Server::startAccept()
{
connections_.push_back(std::make_shared<Connection>(io_service_));
acceptor_.async_accept(connections_.back()->socket(), std::bind(&Server::accept_handler, this, connections_.back(), std::placeholders::_1));
}
void Server::accept_handler(std::shared_ptr<Connection> con, const boost::system::error_code& ec)
{
startAccept();
if (!ec) {
con->start();
}
}
Before I make the call to Server::startAccept, I created an instance of io_service::work and a pool of std::thread which called io_service_.run(). After I make the call to startAccept, the main thread just waits for command line input.
I expect one of the threads in my thread pool to run Server::accept_handler on a connection initiation. This doesn't happen. Instead I have to call io_service_.run() from the main thread.
Now I played around for a while and found that I could achieve the desired behavior by doing this:
void Server::startAccept()
{
connections_.push_back(std::make_shared<Connection>(io_service_));
io_service_.post([this]() { acceptor_.async_accept(connections_.back()->socket(), std::bind(&Server::accept_handler, this, connections_.back(), std::placeholders::_1)); });
}
void Server::accept_handler(std::shared_ptr<Connection> con, const boost::system::error_code& ec)
{
startAccept();
if (!ec) {
con->start();
}
}
What is the difference between the .async_* operations and io_service.post?
EDIT: Defining BOOST_ASIO_ENABLE_HANDLER_TRACKING
When I compile and run my program and then connect to the server with the first block of code I included this is the output:
@asio|1350656555.431359|0*1|socket@00A2F710.async_accept
When I run the second block of code I included and connect to the server I get this output:
@asio|1350656734.789896|0*1|io_service@00ECEC78.post
@asio|1350656734.789896|>1|
@asio|1350656734.789896|1*2|socket@00D0FDE0.async_accept
@asio|1350656734.789896|<1|
@asio|1350656756.150051|>2|ec=system:0
@asio|1350656756.150051|2*3|io_service@00ECEC78.post
@asio|1350656756.150051|>3|
@asio|1350656756.150051|2*4|socket@00EE9090.async_send
@asio|1350656756.150051|3*5|socket@00D0FDE0.async_accept
@asio|1350656756.150051|2*6|socket@00EE9090.async_receive
@asio|1350656756.150051|<3|
@asio|1350656756.150051|>4|ec=system:0,bytes_transferred=54
@asio|1350656756.150051|<2|
@asio|1350656756.150051|<4|
@asio|1350656758.790803|>6|ec=system:10054,bytes_transferred=0
@asio|1350656758.790803|<6|
EDIT 2: Thread creation insight
for (int i = 0; i < NUM_WORKERS; i++) {
thread_pool.push_back(std::shared_ptr<std::thread>(new std::thread([this]() { io_service_.run(); })));
}