其他分享
首页 > 其他分享> > asio使用例子

asio使用例子

作者:互联网

阻塞调用

# 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;
}

 

标签:asio,std,调用,cout,namespace,例子,使用,using,include
来源: https://www.cnblogs.com/xumaomao/p/16653631.html