single ioservice multiple thread Model
来源:互联网 发布:涉案账户资金网络查控 编辑:程序博客网 时间:2024/05/22 16:01
#include "boost/asio.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/function.hpp"
#include "boost/bind.hpp"
#include "boost/shared_ptr.hpp"
#include "boost/thread.hpp"
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
using namespace boost::asio;
class ASYNCIOS
{
public:
ASYNCIOS()
:ptrIosService_(new boost::asio::io_service)
,work(new boost::asio::io_service::work(*ptrIosService_))
,ptrDLTimber_(new boost::asio::deadline_timer(*ptrIosService_,
boost::posix_time::seconds(1)))
{}
void startworking(int seconds)
{
seconds_ = seconds;
ptrDLTimber_->expires_from_now(boost::posix_time::seconds(seconds_));
ptrDLTimber_->async_wait(boost::bind(&ASYNCIOS::async_wait,this));
for( int i = 0; i< 10; i++)
{
boost::shared_ptr<boost::thread> ptrThread
(new boost::thread(
boost::bind(&boost::asio::io_service::run,ptrIosService_) ));
threads_.push_back(ptrThread);
}
// ptrThread_.reset(new boost::thread(
// boost::bind(&boost::asio::io_service::run,ptrIosService_) ));
// ptrThread_->start_thread();
for( int i = 0; i< 10; i++)
threads_[i]->join();
}
void async_wait()
{
cout << "hello world!" << this_thread::get_id()<<endl;
ptrDLTimber_->expires_from_now(boost::posix_time::seconds(seconds_));
ptrDLTimber_->async_wait(boost::bind(&ASYNCIOS::async_wait,this));
}
protected:
private:
typedef boost::shared_ptr<boost::asio::io_service::work> workptr;
boost::shared_ptr<boost::asio::io_service> ptrIosService_;
//boost::shared_ptr<boost::thread> ptrThread_;
boost::shared_ptr<boost::asio::deadline_timer> ptrDLTimber_;
std::vector<boost::shared_ptr<boost::thread> > threads_;
int seconds_;
workptr work;
};
int main()
{
ASYNCIOS ios;
ios.startworking(1); //great it's working!
return 0;
}
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/function.hpp"
#include "boost/bind.hpp"
#include "boost/shared_ptr.hpp"
#include "boost/thread.hpp"
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
using namespace boost::asio;
class ASYNCIOS
{
public:
ASYNCIOS()
:ptrIosService_(new boost::asio::io_service)
,work(new boost::asio::io_service::work(*ptrIosService_))
,ptrDLTimber_(new boost::asio::deadline_timer(*ptrIosService_,
boost::posix_time::seconds(1)))
{}
void startworking(int seconds)
{
seconds_ = seconds;
ptrDLTimber_->expires_from_now(boost::posix_time::seconds(seconds_));
ptrDLTimber_->async_wait(boost::bind(&ASYNCIOS::async_wait,this));
for( int i = 0; i< 10; i++)
{
boost::shared_ptr<boost::thread> ptrThread
(new boost::thread(
boost::bind(&boost::asio::io_service::run,ptrIosService_) ));
threads_.push_back(ptrThread);
}
// ptrThread_.reset(new boost::thread(
// boost::bind(&boost::asio::io_service::run,ptrIosService_) ));
// ptrThread_->start_thread();
for( int i = 0; i< 10; i++)
threads_[i]->join();
}
void async_wait()
{
cout << "hello world!" << this_thread::get_id()<<endl;
ptrDLTimber_->expires_from_now(boost::posix_time::seconds(seconds_));
ptrDLTimber_->async_wait(boost::bind(&ASYNCIOS::async_wait,this));
}
protected:
private:
typedef boost::shared_ptr<boost::asio::io_service::work> workptr;
boost::shared_ptr<boost::asio::io_service> ptrIosService_;
//boost::shared_ptr<boost::thread> ptrThread_;
boost::shared_ptr<boost::asio::deadline_timer> ptrDLTimber_;
std::vector<boost::shared_ptr<boost::thread> > threads_;
int seconds_;
workptr work;
};
int main()
{
ASYNCIOS ios;
ios.startworking(1); //great it's working!
return 0;
}
0 0
- single ioservice multiple thread Model
- android toolchain is using Thread model: single
- 一篇经典的Single-Thread Model原著(节选)
- single model
- multiple thread
- Yii 同一个表,同一个字段,以不同的名字显示 Multiple Labels in a single Model
- Identifying multiple/single values of metadata and recording into XML via .NET Client Object Model
- Single Thread Execution
- Single Thread Execution
- thread in single instance
- linux thread model
- zk event thread model
- multi thread & actor model
- Adding Multiple Views to a Single Document
- Replacing Multiple Patterns in a Single Pass
- Adding Multiple Views to a Single Document
- ADF Single/Multiple files uploading and downloading
- Multiple DataNodes on a single machine
- teiid数据联邦解决方案
- 仿微信语音录制
- ubuntu 如何查看内存?
- android 客户端生成随机验证码的实现
- arcgis webapp builder 安装试用
- single ioservice multiple thread Model
- Sqlserver 字符串分割
- 基于步进电机在LINUX平台下的调试(之一)
- sql server2008 字段类型详解
- Facebook将神奇动画引擎Pop开源了
- java swing g.paint 被调2次 解决方法
- 行外看热闹,行内更热闹——第16届中国智能家居主题沙龙圆满落幕
- 最长回文串算法
- codeigniter源代码分析 - 模型类 Model.php