BOOST-计时器-同步、异步(boost::asio::deadline_timer)

//同步定时器;
{
	boost::asio::io_service ios; //所有asio程序必须要有一个io_service对象;

	boost::asio::deadline_timer d_timer( ios, boost::posix_time::seconds(2) ); //两秒后定时器终止;

	std::cout << d_timer.expires_at() << std::endl; //查看终止的绝对时间;

	d_timer.wait(); //调用wait()同步等待;

	std::cout << "Hello asio" << std::endl;
}
//输出函数
void Print( const boost::system::error_code & )
{
	std::cout << "Hello asio " << std::endl;
}
//异步定时器;
{
	boost::asio::io_service ios; //所有asio程序必须要有一个io_service对象;

	boost::asio::deadline_timer d_timer( ios, boost::posix_time::seconds(2) ); //两秒后定时器终止;

	d_timer.async_wait( Print );

	std::cout << "it show before timer expired." << std::endl;

	ios.run();
}

你可能感兴趣的:(BOOST)