I am using the Boost ASIO library to implement a Windows UDP client which needs to be capable of high throughput.
I would like to use asynchronous receive calls so that I can eventually implement a receive timeout, ie. after a certain amount of time if no datagrams have been received my application will exit.
My problem is that I see 30% higher data throughput using synchronous receives vs. asynchronous receives. I have observed this issue when running the application on multiple Dell R630, R710 Windows 2008 Servers, and even my Lenovo ThinkPad laptop.
What are the main performance differences between the two code segments below?
Is there more overhead in calling ioService.run_one()
after each async receive?
I am a new user of the Boost library, so any help would be much appreciated!
Synchronous Receive:
socket_->receive_from(boost::asio::buffer(&vector_[0], datagramSize),
endPoint_);
vs.
Asynchronous Receive (with blocking):
err = boost::asio::error::would_block;
socket_->async_receive_from(
boost::asio::mutable_buffers_1(&vector_[0], datagramSize),
endPoint_,
boost::bind(&HandleRead, _1, _2, &err, &bytesReceived));
do
{
ioService_.run_one()
}
while(err == boost::asio::error::would_block)
Asynchronous Receive Handler Function:
static void HandleRead
(
const boost::system::error_code& error,
std::size_t bytesRead,
boost::system::error_code* outError,
std::size_t* outBytesRead
)
{
*outError = error;
*outBytesRead = bytesRead;
}