1

我有以下(最小化)类处理我的服务器连接:

class AsioServer {
protected:
    boost::asio::io_service ioService;
public:
    AsioServer() {}

    void add_request() {
        //Adding async requests to the ioService
    }

    void timeout() {
        //Stop all pedning async operations
        ioService.stop();
    }

    void perform() {
        //Set a timer
        boost::asio::deadline_timer timer(ioService);
        timer.expires_from_now(boost::posix_time::seconds(5));
        timer.async_wait(std::bind(&AsioServer::timeout, this));

        //Performe all Async operations
        ioService.run();
        ioService.reset();
    }
};

我的问题是截止日期计时器阻止返回,ioService.run()直到它到期。我想要的是计时器仅在体验然后取消异步操作时才被调用,而不是workio_service. 是否有计时器不作为工作,或者处理这种情况的另一种好方法?

4

1 回答 1

1

io_service::run只要它有一些工作就不会退出(如果没有显式停止) - 这包括任何 i/o 对象和计时器的完成处理程序,因为它io_service单独负责调度所有这些处理程序。

If you don't want the timer to wait anymore after a socket i/o operation completes - just cancel() the timer or re-schedule it, if appropriate. You can find such an approach in asio examples.

于 2013-09-08T18:24:25.877 回答