#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;
}
single ioservice multiple thread Model
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
C++ primer 閱讀日記
fridayshi
2018-08-27 01:26:29
c, c++, Beep, 喇叭 聲音處理
fridayshi
2018-08-27 01:26:27
Const,Const函數,Const變量,函數後面的Const(轉)
fridayshi
2018-08-27 01:26:20
Visual Studio 2005/2008中的快捷鍵與小技巧整理
fridayshi
2018-08-27 01:26:20
C語言中操作符的優先級大全
Jabez
2018-08-22 21:29:06
利用電腦探討中國古代益智遊戲─「華容道」之解法
suprman
2018-08-22 01:52:21
最新文章