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