简体   繁体   English

无法解除阻止boost asio受体

[英]Could not unblock the boost asio acceptor

I am trying to cancel a TCP acceptor by the programs keeps blocked in that line. 我试图通过程序在该行保持阻塞的状态来取消TCP接受器。

One thread waits for connections like this: 一个线程等待这样的连接:

boost::system::error_code ec;
acceptor_ = new boost::asio::ip::tcp::acceptor(io_service_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
acceptor_->accept(socket_, ec);  // socket_ is a boost::asio::ip::tcp:socket

The thread that must close the acceptor does: 必须关闭接受器的线程会执行以下操作:

boost::system::error_code ec;
acceptor_->close(ec);

Is there something I am missing? 我有什么想念的吗? I am not using any async operation, so may I do something with the io_service object? 我没有使用任何异步操作,因此我可以对io_service对象做些什么吗?

In short, you cannot cancel most of async operations using asio function and methods. 简而言之,您无法使用asio函数和方法取消大多数异步操作。

The problem is underlying accept , read etc are system calls. 问题是底层的acceptread等是系统调用。 They cannot be interrupted using io_service::stop or cancel since accepting thread is blocked inside syscall, not asio event loop. 它们不能使用io_service::stopcancel中断,因为接受线程被阻塞在syscall而不是asio事件循环中。 They only can be interrupted by killing thread or sending a signal. 它们只能通过杀死线程或发送信号来中断。

I'd prefer to make this acceptor asyncronous, so problem will gone. 我希望使该接收器异步,因此问题将消失。 Also see this Q on SO about this 另请参阅有关此问题的Q

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM