boost学习--timer

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">开始认真学习boost库,先看看timer的工作。</span>

有同步,异步之分。网上找了个例子,只有片段代码,好吧,把它编译通过,大致什么意思就明白了。

下面是3个主方法:

	// 同步方法
	void test_timer_syn()
	{
		boost::asio::io_service ios;
		boost::asio::deadline_timer t(ios, boost::posix_time::seconds(3));

		PRINT_DEBUG2("timer expires_at: ",t.expires_at());

		t.wait();
		PRINT_DEBUG("Hello syn deadline_timer!");
	}

	// 异步方法: 3秒后执行print方法.
	void test_timer_asyn()
	{
		boost::asio::io_service io;

		boost::asio::deadline_timer t(io, boost::posix_time::seconds(3));
		t.async_wait(print);
		PRINT_DEBUG("After async_wait...");
		io.run();
	}

	// 异步循环执行方法:
	void test_timer_asyn_loop()
	{
		boost::asio::io_service io;
		boost::asio::deadline_timer t(io);
		size_t a = t.expires_from_now(boost::posix_time::seconds(1));

		int count = 0;
		t.async_wait(boost::bind(handle_wait,
								 boost::asio::placeholders::error,
								 boost::ref(t),
								 boost::ref(count)));
		io.run();

		PRINT_DEBUG("after io.run");
		//PRINT_DEBUG(now_str);
	}

再看异步方法中用到的方法:

   void print(const boost::system::error_code&)
    {
        PRINT_DEBUG("Hello, world!");
    }

    void handle_wait(const boost::system::error_code& error,
                     boost::asio::deadline_timer& t,
                     int& count)
    {
        if(!error)
        {
            PRINT_DEBUG(count);
            if(count++ < 5)
            {
                t.expires_from_now(boost::posix_time::seconds(3));
                t.async_wait(boost::bind(handle_wait,
                                         boost::asio::placeholders::error,
                                         boost::ref(t),
                                         boost::ref(count)));
                if (count == 3)
                {
                    t.cancel();
                }

            }
        }
    }

具体程序:

test_timer.hpp:

//#include "boost/lexical_cast.hpp"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <string>
#include <iostream>
//#include <stdlib.h>

using std::cout;
using namespace boost::posix_time;

#define PRINT_DEBUG(obj)\
{\
	cout<<microsec_clock::local_time() << " : "<< obj << std::endl; \
}\

#define PRINT_DEBUG2(obj1,obj2)\
{\
	cout<<microsec_clock::local_time() << " : "<< obj1 <<obj2 << std::endl; \
}\

namespace  TEST
{
/*
	template <typename T>
	std::string to_string(const T& arg)
	{
		try
		{
			return boost::lexical_cast<std::string>(arg);
		}
		catch(boost::bad_lexical_cast& e)
		{
			return "";
		}
	}
*/

    void print(const boost::system::error_code&)
    {
        PRINT_DEBUG("Hello, world!");
    }

    void handle_wait(const boost::system::error_code& error,
                     boost::asio::deadline_timer& t,
                     int& count)
    {
        if(!error)
        {
            PRINT_DEBUG(count);
            if(count++ < 5)
            {
                t.expires_from_now(boost::posix_time::seconds(3));
                t.async_wait(boost::bind(handle_wait,
                                         boost::asio::placeholders::error,
                                         boost::ref(t),
                                         boost::ref(count)));
                if (count == 3)
                {
                    t.cancel();
                }

            }
        }
    }

	// 同步方法
	void test_timer_syn()
	{
		boost::asio::io_service ios;
		boost::asio::deadline_timer t(ios, boost::posix_time::seconds(3));

		PRINT_DEBUG2("timer expires_at: ",t.expires_at());

		t.wait();
		PRINT_DEBUG("Hello syn deadline_timer!");
	}

	// 异步方法: 3秒后执行print方法.
	void test_timer_asyn()
	{
		boost::asio::io_service io;

		boost::asio::deadline_timer t(io, boost::posix_time::seconds(3));
		t.async_wait(print);
		PRINT_DEBUG("After async_wait...");
		io.run();
	}

	// 异步循环执行方法:
	void test_timer_asyn_loop()
	{
		boost::asio::io_service io;
		boost::asio::deadline_timer t(io);
		size_t a = t.expires_from_now(boost::posix_time::seconds(1));

		int count = 0;
		t.async_wait(boost::bind(handle_wait,
								 boost::asio::placeholders::error,
								 boost::ref(t),
								 boost::ref(count)));
		io.run();

		PRINT_DEBUG("after io.run");
		//PRINT_DEBUG(now_str);
	}

}

main程序:

#include "test_timer.hpp"
//#define PRINT_DEBUG(format,...) printf("File: "__FILE__", Line: %05d: "format"/n", __LINE__, ##__VA_ARGS__)
using namespace TEST;

int main(int argc, char* argv[])
{
	PRINT_DEBUG("START TEST_TIMER test_timer_syn");
	test_timer_syn();
	PRINT_DEBUG("END TEST_TIMER test_timer_syn");

	PRINT_DEBUG("START TEST_TIMER test_timer_asyn");
	test_timer_asyn();
	PRINT_DEBUG("END TEST_TIMER test_timer_asyn");

	PRINT_DEBUG("START TEST_TIMER test_timer_asyn_loop");
	test_timer_asyn_loop();
	PRINT_DEBUG("END TEST_TIMER test_timer_asyn_loop");
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值