#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
//asio同步定时器
int main()
{
//所有使用asio的程序都要创建一个io_service对象,这个对象提供io功能支持
boost::asio::io_service io_service;
//定义一个定时器对象,超时时间设置为5秒
boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(5));
//这是在定时器上阻塞等待的简单范例.调用deadline_timer::wait()会在定时器到期(5秒钟)后才返回.
timer.wait();
//定时器5秒到期后执行下面的语句
cout<<"timeout!"<<endl;
system("pause");
return 0;
}
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
//异步等待调用的回调函数,等待时间到期后调用
void sync_wait_handle(const boost::system::error_code &error_code)
{
cout<<"async_wait() callback"<<endl;
cout<<"error_code : "<<error_code<<endl;
}
//asio异步定时器
int main()
{
boost::asio::io_service io_service;
boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(5));
timer.async_wait(&sync_wait_handle);
//最后,必须在io_service对象上调用io_service::run()成员函数.
//Asio库保证回调函数在调用了io_service::run()的线程上执行.
//因此只有调用了io_service::run()函数,异步等待完成的回调函数才能被调用.
//Io_service::run()将会一直在运行,因为其还有工作要做.
//本例中,这个工作是异步等待定时器,因此直到定时器过期而且回调函数执行完毕,才会退出.
//在调用io_service::run()前为其指定工作是很重要的.
//例如,如果上例中忘记调用deadline_timer::async_wait(),io_service无事可做,
//则io_service::run()调用会立即返回.
io_service.run();
system("pause");
return 0;
}
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/bind.hpp>
using namespace std;
void print_handle(const boost::system::error_code &error_code,
boost::asio::deadline_timer *timer, int *count)
{
if (*count < 5)
{
cout<<*count<<endl;
++(*count);
//将过期时间从前一个过期时间向后延迟一秒.在原来基础上定义新的过期时间,
//可以确保定时器标记的秒数不会由于处理函数执行而延时
timer->expires_at(timer->expires_at() + boost::posix_time::seconds(1));
timer->async_wait(boost::bind(print_handle, boost::asio::placeholders::error, timer, count));
}
}
//asio向处理函数传递参数
int main()
{
boost::asio::io_service io_service;
int count = 0;
boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(5));
//向回调函数传递参数
timer.async_wait(boost::bind(print_handle, boost::asio::placeholders::error, &timer, &count));
io_service.run();
cout<<"final count is "<<count<<endl;
system("pause");
return 0;
}
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/bind.hpp>
using namespace std;
class Printer
{
public:
Printer(boost::asio::io_service &io_service)
:timer_(io_service, boost::posix_time::seconds(3)), count_(0)
{
timer_.async_wait(boost::bind(&Printer::print, this));
}
~Printer()
{
cout<<"destroy printer, final count_ is "<<count_<<endl;
}
void print()
{
if (count_ < 5)
{
cout<<count_<<endl;
++count_;
timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
timer_.async_wait(boost::bind(&Printer::print, this));
}
}
private:
boost::asio::deadline_timer timer_;
int count_;
};
//asio将类的成员函数作为回调处理函数
int main()
{
boost::asio::io_service io_service;
Printer printer(io_service);
io_service.run();
system("pause");
return 0;
}