boost::asio学习(定时器)

#include <boost/asio.hpp>
#include <iostream>

void handle1(const boost::system::error_code& ec)
{
    std::cout << "5.s" << std::endl;
}

void handle2(const boost::system::error_code& ec)
{
    std::cout << "10.s" << std::endl;
}

int main()
{
    boost::asio::io_service io_service;//io服务
    boost::asio::deadline_timer timer1(io_service, boost::posix_time::seconds(5));//定时器timer1
    timer1.async_wait(handle1);//异步等待
    boost::asio::deadline_timer timer2(io_service, boost::posix_time::seconds(10));//定时器timer2
    timer2.async_wait(handle2);//异步等待
    io_service.run();//阻塞调用,这个是必须的,这样才能将控制权交给操作系统,可以再一个线程里面阻塞,这个和libevent派发线程很像
}

 

posted @ 2015-03-18 13:31  zzyoucan  阅读(194)  评论(0编辑  收藏  举报