[英]Boost.Asio, two readings by the same socket
我正在学习Boost.Asio。 我定义了一个具有客户端和服务器角色的类:
我希望能够在订阅后发出请求并接收响应,但是当我收到响应时出现了一个问题:在同一时间等待的套接字同时收到了订阅和响应。
有时会有死锁,有时我在等待响应时得到订阅,...
要阅读,我使用以下功能:
void async_receive_subscription(const socket& the_socket, read_handler_signature handler)
{
auto self( this->shared_from_this() );
boost::asio::async_read(
the_socket.get(),
boost::asio::buffer( my_buffer ),
[this, self, the_socket, my_buffer, handler]
( const boost::system::error_code& error,
std::size_t bytes_transferred )
{
the_socket.get().get_io_service().post(
std::bind(
handler,
error,
bytes_transferred,
the_socket,
my_buffer
)
);
// Read the next subscription.
async_receive_subscription(the_socket, handler);
}
);
}
void async_receive(const socket& the_socket, read_handler_signature handler)
{
auto self( this->shared_from_this() );
boost::asio::async_read(
the_socket.get(),
boost::asio::buffer( my_buffer ),
[this, self, the_socket, my_buffer, handler]
( const boost::system::error_code& error,
std::size_t bytes_transferred )
{
the_socket.get().get_io_service().post(
std::bind(
handler,
error,
bytes_transferred,
the_socket,
my_buffer
)
);
}
);
}
是否可以期望同一套接字有两个响应? 是否可以使用任何同步机制来避免此问题?
谢谢。
确保async_read恰好引起处理程序的一次调用。
在调用此处理程序之前,不得再次调用async_read。
题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.