Error

C++博客 首页 新随笔 联系 聚合 管理
  217 Posts :: 61 Stories :: 32 Comments :: 0 Trackbacks
// learn_boost_asio.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <memory>
#include <boost/asio.hpp>
#include <boost/timer.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
namespace asio = boost::asio;
namespace posix_time = boost::posix_time ;
// 1.同步定时器使用
void LeanrHowToUseSynchronouslyTimer()
{
asio::io_service io;
asio::deadline_timer deadLineTimer(io, posix_time::seconds(3));
boost::timer timerTestDeadLineTimer;
timerTestDeadLineTimer.restart();
deadLineTimer.wait();
double dTime = timerTestDeadLineTimer.elapsed();
std::cout << dTime << std::endl;
}
////////~
// 2.异步定时器使用
class CSomeObject
{
public:
CSomeObject(asio::io_service& ioService) :
   m_deadLineTimer(ioService)
{
}
public:
void Test()
{
m_deadLineTimer.expires_from_now(boost::posix_time::seconds(3));
m_timerTestDeadTimer.restart();
m_deadLineTimer.async_wait(
boost::bind(&CSomeObject::TimerHandler, this, asio::placeholders::error));
}
public:
void TimerHandler(const boost::system::error_code&)
{
double dTime = m_timerTestDeadTimer.elapsed();
std::cout << "time passed: " << dTime << std::endl;
}
private:
boost::timer m_timerTestDeadTimer;
asio::deadline_timer m_deadLineTimer;
};
void LearnHowToUseAsynchronouslyTimer()
{
asio::io_service ioService;
CSomeObject obj(ioService);
obj.Test();
asio::io_service::work* pWork = new asio::io_service::work(ioService);
std::auto_ptr<asio::io_service::work> spWork(pWork);
ioService.run();
//spWork.reset(); // Allow run() to exit. 
}
//////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
LearnHowToUseAsynchronouslyTimer();
return 0;
}
posted on 2013-03-08 18:04 Enic 阅读(769) 评论(0)  编辑 收藏 引用 所属分类: boost::asio

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理