boost::asio定时器

// 1. 同步定时器
#include <cstdio>
#include <iostream>
#include <boost/asio.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
 boost::asio::io_service io;
 boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
 t.wait();//同步定时器,5后被调用
 std::cout << "Hello, world!\n";
 return 0;
}

链接库 -lpthread  -lboost_system
// 2. 异步定时器
#include <cstdio>
#include <iostream>
#include <boost/asio.hpp> 
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;

/****************************************************************
函  数:Print
参  数:void
返回值:const boost::system::error_code &ec   -- 执行失败原因
说  明:回调函数
****************************************************************/
void Print(const boost::system::error_code &ec)
{
    cout << "Hello World!" << endl;
    cout << boost::this_thread::get_id() << endl;//id一致, 在同一个线程中执行
}

int main()
{
    cout << boost::this_thread::get_id() << endl;//id一致, 在同一个线程中执行
    boost::asio::io_service io;
    boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
    t.async_wait(Print);//异步定时器,立马返回
    cout << "to run" << endl;
    io.run(); //异步IO调度
    cout << "exit" << endl;

    return EXIT_SUCCESS;
}
// 3.  定时器异步递归回调,使用 deadline_timer 实现周期性任务执行(类似“每隔5秒执行一次”),并控制执行次数。
void Print(const boost::system::error_code &ec, boost::asio::deadline_timer* pt, int* pcount)
{
    if (*pcount < 3)
    {
        cout << "count = " << *pcount << endl;
        (*pcount)++;
        //推迟定时器的终止时间,确保回调函数不会在所需时间内到期
        pt->expires_at(pt->expires_at() + boost::posix_time::seconds(5));
        //重新绑定任务
        pt->async_wait(boost::bind(Print, boost::asio::placeholders::error, pt, pcount));
    }
}

int main()
{
    boost::asio::io_service io;
    boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
    int count = 0;
    t.async_wait(boost::bind(Print, boost::asio::placeholders::error, &t, &count));
    cout << "to run" << endl;
    io.run();
    cout << "Final count is " << count << "\n";//3
    cout << "exit" << endl;
    return 0;
}
// 4、多线程 + Boost.Asio + strand 串行化,
//   在多线程环境中使用 boost::asio::strand 来保证回调函数的串行执行和线程安全。
#include <cstdio>
#include <iostream>
#include <boost/asio.hpp> 
#include <boost/thread.hpp>
#include <boost/asio/strand.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost;
using namespace std;
class CPrinter
{
public:
    CPrinter(boost::asio::io_service &io)
        :m_strand(io)
        , m_timer1(io, boost::posix_time::seconds(5))
        , m_timer2(io, boost::posix_time::seconds(5))
        , m_count(0)
    {
        m_timer1.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print1, this)));
        m_timer2.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print2, this)));
    }
    ~CPrinter()
    {
        cout << "m_count = " << m_count << endl;
    }
    void Print1()
    {
        if (m_count < 10)
        {
            cout << "Timer1 count = " << m_count << endl;
            cout << boost::this_thread::get_id() << endl;
            m_count++;
            m_timer1.expires_at(m_timer1.expires_at() + boost::posix_time::seconds(1));
            m_timer1.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print1, this)));
        }
    }
    void Print2()
    {
        if (m_count < 10)
        {
            cout << "Timer2 count = " << m_count << endl;
            cout << boost::this_thread::get_id() << endl;
            m_count++;
            m_timer2.expires_at(m_timer2.expires_at() + boost::posix_time::seconds(1));
            m_timer2.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print2, this)));
        }
    }
private:
    //strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发
    boost::asio::strand m_strand;
    boost::asio::deadline_timer m_timer1;
    boost::asio::deadline_timer m_timer2;
    int m_count;
};

int main()
{
    cout << boost::this_thread::get_id() << endl;
    boost::asio::io_service io;
    CPrinter cp(io);
    cout << "to run" << endl;
    boost::thread td(boost::bind(&boost::asio::io_service::run, &io));
    io.run();
    cout << "exit" << endl;

    return EXIT_SUCCESS;
}
posted @ 2018-11-27 22:11  osbreak  阅读(459)  评论(0)    收藏  举报