- #include <iostream>
- #include <boost/asio.hpp>
- int main ()
- {
- Boost::asio::io_service io;
- Boost::asio::d eadline_timer timer (io, boost::p osix_time::seconds (3));
- Timer.wait ();
- Std::cout << "Hello, world!\n";
- return 0;
- }
One more asynchronous:
[CPP]View Plaincopy
- void print (const boost::system::error_code& /*e*/)
- {
- Std::cout << "Hello, world!\n";
- }
- int main ()
- {
- Boost::asio::io_service io;
- Boost::asio::d eadline_timer timer (io, boost::p osix_time::seconds (5));
- Timer.async_wait (&print);
- Io.run ();
- return 0;
- }
The basic usage was demonstrated last time, but it only happens once.
The question is how to define a recurring timer (it happens once in a certain time). , the following code is, the key is that the callback function changes the delay constant extension timer
[CPP]View Plaincopy
- void print (const boost::system::error_code& E,
- Boost::asio::d eadline_timer* t)
- {
- cout<<"DDD" <<endl;
- T->expires_at (T->expires_at () + boost::p osix_time::seconds (1));
- T->async_wait (Boost::bind (print,boost::asio::p laceholders::error,t));
- }
- void Test1 ()
- {
- Boost::asio::io_service io;
- Boost::asio::d eadline_timer t (io, boost::p osix_time::seconds (1));
- T.async_wait (Boost::bind (print,boost::asio::p laceholders::error,&t));
- Io.run ();
- }
- int _tmain (int argc, _tchar* argv[])
- {
- Test1 ();
- System ("pause");
- return 0;
- }
Boost timer.