source: experimental/Threading/Timer/main.cpp @ 429

Last change on this file since 429 was 429, checked in by Torben Dannhauer, 11 years ago
File size: 1.9 KB
Line 
1// Timer Test
2
3#define _WIN32_WINNT  0x0601
4
5// OSG includes
6#include <osg/Notify>
7#include <osg/Timer>
8#include <boost/asio.hpp>
9#include <boost/thread.hpp>
10
11
12
13using namespace std;
14
15class timed_class
16{
17public:
18  timed_class(boost::asio::io_service& io_service) : _timer(io_service), i(0), timer(*osg::Timer::instance())
19  {
20        last_tick = timer.tick();
21
22    // now schedule the first timer.
23
24    _timer.expires_from_now(boost::posix_time::milliseconds(1000/60)); // runs with 60Hz
25    _timer.async_wait(boost::bind(&timed_class::handle_timeout, this, boost::asio::placeholders::error));
26  }
27
28  void handle_timeout(boost::system::error_code const& cError)
29  {
30    if (cError.value() == boost::asio::error::operation_aborted)
31      return;
32
33    if (cError && cError.value() != boost::asio::error::operation_aborted)
34      return; // throw an exception?
35
36        // Schedule the timer again...
37    _timer.expires_from_now(boost::posix_time::milliseconds(1000/60)); // runs with 60Hz
38    _timer.async_wait(boost::bind(&timed_class::handle_timeout, this, boost::asio::placeholders::error));
39
40
41        // Payload
42        osg::Timer_t currentTick = timer.tick();
43        cout << timer.delta_m(last_tick,currentTick) << " msec ("<<(1000/60)<<")" << endl;
44        last_tick = timer.tick();
45  }
46
47private:
48  boost::asio::deadline_timer _timer;
49  boost::asio::basic_deadline_timer<> _timer;
50  int i;
51  const osg::Timer& timer;
52  osg::Timer_t last_tick;
53};
54
55
56
57int main( int argc, char **argv )
58{
59        boost::asio::io_service io_service;
60
61        timed_class t(io_service);
62
63        //io_service.run();     // Start ioService in this thread( blocking
64
65        // Start Timer in another thread to continue this thread
66        boost::thread thread(boost::bind(&boost::asio::io_service::run, &io_service));
67
68        // OUtput stuff to show th thread is working
69        while(1)
70                //OSG_ALWAYS<<"Main Thread:"<<i<<std::endl;
71                OSG_ALWAYS<<"";
72
73    return 0;
74}
Note: See TracBrowser for help on using the repository browser.