Remove all handlers from a boost::asio::io_service without calling them

后端 未结 1 485
别跟我提以往
别跟我提以往 2021-01-04 10:59

I want to remove all handlers from an IO_service right before I reuse it. Is this possible?

I\'m writing unit tests that involve an asio::io_service.

相关标签:
1条回答
  • 2021-01-04 11:34

    Well, I racked my brain on this for a few days and came up with a workable solution. It's the mother of all hacks.

    void clear( boost::asio::io_service& service )
    {
        service.stop();
        service.~io_service();
        new( &service ) boost::asio::io_service;
    }
    

    I'm not sure how safe this would be for productions code. But so far it seems to work (no segfaults, no weird behavior).

    0 讨论(0)
提交回复
热议问题