|
Boost Users : |
Subject: Re: [Boost-users] boost::asio blocking socket read with timeout with multiple threads
From: Jeremi Piotrowski (jeremi.piotrowski_at_[hidden])
Date: 2018-03-24 18:27:17
On Fri, Mar 23, 2018 at 11:42:02AM +0000, Thomas Quarendon via Boost-users wrote:
> > Yeah, it's a mess compared to Windows CancelSynchronousIO
> Quite. Yuck!
>
> All I want is to be able to pass a timeout to the poll call that asio makes on my behalf! The operating system gives me what I want, I just can't get at it.
What if you use boost::asio's future support.
Your BlockingConnection::start() method then looks like this:
void BlockingConnection::start()
{
std::vector<char> b(1024);
std::future<std::size_t> future = socket_.async_read_some(
buffer(b),
boost::asio::use_future);
if (future.wait_for(std::chrono::milliseconds{1000}) ==
std::future_status::timeout) {
std::cout << "BlockingConnection terminating " << std::endl;
socket_.close();
} else {
// get() may throw
std::cout << "read bytes: " << future.get() << std::endl;
}
}
This seems to be the closest to what you need. But it still requires that
you not wait_for() from within the same io_service. So what you could do
is have two io_services, one for all actual async operations with a thread
blocked on run(), and a second io_service with as many threads as
connections that you want to handle concurrently with sync reads. You
would accept in the first io_service, and then
.post(BlockingConnection::start) into the second one.
Seems like this could work.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net