Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the following class:</p> <pre><code>class timer { typedef boost::signals2::signal&lt;void ()&gt; timeout_slot; public: typedef timeout_slot::slot_type timeout_slot_t; public: timer() : _interval(0), _is_active(false) {}; timer(int interval) : _interval(interval), _is_active(false) {}; virtual ~timer() { stop(); }; inline boost::signals2::connection connect(const timeout_slot_t&amp; subscriber) { return _signalTimeout.connect(subscriber); }; void start() { boost::lock_guard&lt;boost::mutex&gt; lock(_guard); if (is_active()) return; // Already executed. if (_interval &lt;= 0) return; _timer_thread.interrupt(); _timer_thread.join(); timer_worker job; _timer_thread = boost::thread(job, this); _is_active = true; }; void stop() { boost::lock_guard&lt;boost::mutex&gt; lock(_guard); if (!is_active()) return; // Already executed. _timer_thread.interrupt(); _timer_thread.join(); _is_active = false; }; inline bool is_active() const { return _is_active; }; inline int get_interval() const { return _interval; }; void set_interval(const int msec) { if (msec &lt;= 0 || _interval == msec) return; boost::lock_guard&lt;boost::mutex&gt; lock(_guard); // Keep timer activity status. bool was_active = is_active(); if (was_active) stop(); // Initialize timer with new interval. _interval = msec; if (was_active) start(); }; protected: friend struct timer_worker; // The timer worker thread. struct timer_worker { void operator()(timer* t) { boost::posix_time::milliseconds duration(t-&gt;get_interval()); try { while (1) { boost::this_thread::sleep&lt;boost::posix_time::milliseconds&gt;(duration); { boost::this_thread::disable_interruption di; { t-&gt;_signalTimeout(); } } } } catch (boost::thread_interrupted const&amp; ) { // Handle the thread interruption exception. // This exception raises on boots::this_thread::interrupt. } }; }; protected: int _interval; bool _is_active; boost::mutex _guard; boost::thread _timer_thread; // Signal slots timeout_slot _signalTimeout; }; </code></pre> <p>An example of usage:</p> <pre><code>void _test_timer_handler() { std::cout &lt;&lt; "_test_timer_handler\n"; } BOOST_AUTO_TEST_CASE( test_timer ) { emtorrus::timer timer; BOOST_CHECK(!timer.is_active()); BOOST_CHECK(timer.get_interval() == 0); timer.set_interval(1000); timer.connect(_test_timer_handler); timer.start(); BOOST_CHECK(timer.is_active()); std::cout &lt;&lt; "timer test started\n"; boost::this_thread::sleep&lt;boost::posix_time::milliseconds&gt;(boost::posix_time::milliseconds(5500)); timer.stop(); BOOST_CHECK(!timer.is_active()); BOOST_CHECK(_test_timer_count == 5); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
 

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