Implementing an event timer using boost::asio

后端 未结 3 806
暖寄归人
暖寄归人 2021-01-24 15:42

The sample code looks long, but actually it\'s not so complicated :-)

What I\'m trying to do is, when a user calls EventTimer.Start(), it will execute the callba

相关标签:
3条回答
  • 2021-01-24 16:28

    As Sam hinted, depending on what you're attempting to accomplish, most of the time it is considered a design error to stop an io_service. You do not need to stop()/reset() the io_service in order to reschedule a timer.

    Normally you would leave a thread or thread pool running attatched to an io_service and then you would schedule whatever event you need with the io_service. With the io_service machinery in place, leave it up to the io_service to dispatch your scheduled work as requested and then you only have to work with the events or work requests that you schedule with the io_service.

    0 讨论(0)
  • 2021-01-24 16:30

    I figured it out, but I don't know why that I have to put io.reset() in Start(), since it's already been called in Stop().

    See the updated code in the post.

    0 讨论(0)
  • 2021-01-24 16:31

    It's not entirely clear to me what you are trying to accomplish, but there's a couple of things that are incorrect in the code you have posted.

    1. io_service::reset() should only be invoked after a previous invocation of io_service::run() was stopped or ran out of work as the documentation describes.
    2. you should not need explicit calls to Sleep(), the call to io_service::run() will block as long as it has work to do.
    0 讨论(0)
提交回复
热议问题