Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The asio <code>io_service</code> runs in its own thread, once you <em>schedule</em> a timer event, it will call you back at the appropriate time.</p> <p>Here is a complete example:</p> <pre><code>#include &lt;iostream&gt; #include &lt;boost/asio.hpp&gt; #include &lt;boost/bind.hpp&gt; using namespace std; class timed_class { public: timed_class(boost::asio::io_service&amp; io_service) : _timer(io_service) { // now schedule the first timer. _timer.expires_from_now(boost::posix_time::milliseconds(100)); // runs every 100 ms _timer.async_wait(boost::bind(&amp;timed_class::handle_timeout, this, boost::asio::placeholders::error)); } void handle_timeout(boost::system::error_code const&amp; cError) { if (cError.value() == boost::asio::error::operation_aborted) return; if (cError &amp;&amp; cError.value() != boost::asio::error::operation_aborted) return; // throw an exception? cout &lt;&lt; "timer expired" &lt;&lt; endl; // Schedule the timer again... _timer.expires_from_now(boost::posix_time::milliseconds(100)); // runs every 100 ms _timer.async_wait(boost::bind(&amp;timed_class::handle_timeout, this, boost::asio::placeholders::error)); } private: boost::asio::deadline_timer _timer; }; int main(void) { boost::asio::io_service io_service; timed_class t(io_service); io_service.run(); return 0; } </code></pre> <p>EDIT: to dispatch in a separate thread, </p> <pre><code>boost::thread t(boost::bind(&amp;boost::asio::io_service::run, &amp;io_service)); </code></pre> <p>Instead of <code>io_service.run();</code>, now the main thread will continue and once the timeout has expired, then the <code>io_service</code> will call <code>handle_timeout</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks for your reply. My question is I need to call the io_service.run() in the main thread. However, in the code between timed_class t(io_service) and io_service.run(), I need to perform other task. If I call io_service.run() first, it would wait for the delay first before performing the task. If I call the function to perform the task before the io_service.run(), I would end up needing to know the time needed for the task to complete to call io_service.run() at appropriate time, which is my original problem. So either way doesn't solve my problem. Or am I missing something? Thanks.
      singulars
    2. CO@Ashley, `io_service::run()` is blocking whilst there are events to *dispatch*, if you need to do some setup before the code that needs to be executed periodically, you should do that first then call the `run()` to periodically call the `handle_timer` function. If the code you want to execute *is the code* that needs to be called periodically, put that in a function in the `timed_class`, and then call that in the ctor and the `handle_timeout` body. It may be better if you put some pseudo code of what you want to achieve...
      singulars
    3. COThanks for your reply. Let me provide the base case. Let say I need to call functionA only once after 300 ms. I want to pass the functionA into some timer callback which would call that after exactly 300ms (T1), something like what TokenMacGuy's solution provided above. At current time (T0), if I call io_service::run(), it would block until (T1), which is undesirable as I don't want the main thread to block. If I were to call io_service::run() later, I couldn't estimate when is exactly 300 ms later, which is the original problem of calling function after 300 ms.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload