阻塞调用
# include<iostream> # include<boost/asio.hpp> # include<boost/date_time/posix_time/posix_time.hpp> using namespace std; int main() { cout << "One" << endl; boost::asio::io_service io; boost::asio::deadline_timer t(io, boost::posix_time::seconds(5)); t.wait(); // 阻塞等待5秒 cout << "two" << endl; }
异步调用
# include<iostream> # include<boost/asio.hpp> # include<boost/date_time/posix_time/posix_time.hpp> using namespace std; void callback(const boost::system::error_code&) { cout << "run" << endl; } int main() { cout << "One" << endl; boost::asio::io_service io; boost::asio::deadline_timer t(io, boost::posix_time::seconds(5)); t.async_wait(callback); // 事件注册,5s钟后调用callback cout << "two" << endl; io.run(); // 这里才真正执行 cout << "three" << endl; }