boost高并发网络框架+线程池

from: http://blog.csdn.net/herostarone/article/details/7526456

//有几个地方改了下,要不编译不过

#include
//#include "AuthenHandle.h"  // 与原来的不同
//#include "configure.h" // 与原来的不同
#ifdef WIN32 //for windows nt/2000/xp


//#include
#include
// 需要在stdafx.h 文件加入:#define WIN32_LEAN_AND_MEAN  // 与原来的不同
// 否则: fatal error C1189: #error :  WinSock.h has already been included
//#include "gelsserver.h"
//#pragma comment(lib,"Ws2_32.lib")
#else         //for unix

#include
//    #include
//    #include
//    #include
#include     //socket
//    #include
#include            //gethostname
// #include
#include
#include            //memset

typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#ifdef M_I386
typedef int socklen_t;
#endif

#define BOOL             int
#define INVALID_SOCKET    -1
#define SOCKET_ERROR     -1
#define TRUE             1
#define FALSE             0
#endif        //end #ifdef WIN32

static int count111 = 0;
static time_t oldtime = 0, nowtime = 0;

#include
#include
#include
#include
#include
#include
#include

using namespace std;
using boost::asio::ip::tcp;

class io_service_pool
: public boost::noncopyable
{
public:

explicit io_service_pool(std::size_t pool_size)
: next_io_service_(0)

for (std::size_t i = 0; i < pool_size; ++ i)
{
io_service_sptr io_service(new boost::asio::io_service);
work_sptr work(new boost::asio::io_service::work(*io_service));
io_services_.push_back(io_service);
work_.push_back(work);
}
}

void start()

for (std::size_t i = 0; i < io_services_.size(); ++ i)
{
boost::shared_ptr thread(new boost::thread(
boost::bind(&boost::asio::io_service::run, io_services_[i])));
threads_.push_back(thread);
}
}

void join()
{
for (std::size_t i = 0; i < threads_.size(); ++ i)
{
threads_[i]->join();

}

void stop()

for (std::size_t i = 0; i < io_services_.size(); ++ i)
{
io_services_[i]->stop();
}
}

boost::asio::io_service& get_io_service()
{
boost::mutex::scoped_lock lock(mtx);
boost::asio::io_service& io_service = *io_services_[next_io_service_];
++ next_io_service_;
if (next_io_service_ == io_services_.size())
{
next_io_service_ = 0;
}
return io_service;
}

private:
typedef boost::shared_ptr io_service_sptr;
typedef boost::shared_ptr work_sptr;
typedef boost::shared_ptr thread_sptr;

boost::mutex mtx;

std::vector io_services_;
std::vector work_;
std::vector threads_; 
std::size_t next_io_service_;
};

boost::mutex cout_mtx;
int packet_size = 0;
enum {MAX_PACKET_LEN = 4096};

class session
{
public:
session(boost::asio::io_service& io_service)
: socket_(io_service)
, recv_times(0)
{
}

virtual ~session()
{
boost::mutex::scoped_lock lock(cout_mtx);
}

tcp::socket& socket()
{
return socket_;
}

inline void start()
{

socket_.async_read_some(boost::asio::buffer(data_, MAX_PACKET_LEN),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}

void handle_read(const boost::system::error_code& error, size_t bytes_transferred)
{
if (!error)
{
++ recv_times;

count111 ++;

struct tm *today;
time_t ltime;
time( &nowtime );
if(nowtime != oldtime){
printf("%d\n", count111);
oldtime = nowtime;
count111 = 0;
}
boost::asio::async_write(socket_, boost::asio::buffer(data_, bytes_transferred),
boost::bind(&session::handle_write, this, boost::asio::placeholders::error));
}
else
{
delete this;
}
}
void handle_write(const boost::system::error_code& error)
{
if (!error)
{
start();
}
else
{
delete this;
}
}

private:
tcp::socket socket_;
char data_[MAX_PACKET_LEN];
int recv_times;
};

class server
{
public:
server(short port, int thread_cnt)
: io_service_pool_(thread_cnt)
, acceptor_(io_service_pool_.get_io_service(), tcp::endpoint(tcp::v4(), port))
{
session* new_session = new session(io_service_pool_.get_io_service());
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error));
}

void handle_accept(session* new_session, const boost::system::error_code& error)
{
if (!error)
{
new_session->start();
}
else
{
delete new_session;
}

new_session = new session(io_service_pool_.get_io_service());
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error));
}

void run()
{
io_service_pool_.start();
io_service_pool_.join(); // 为什么药join 这与单线程有什么区别
}
private:
io_service_pool io_service_pool_;
tcp::acceptor acceptor_;
};

int main()
{

//boost

server s(1532, 50);
s.run();

while(true)
{
Sleep(1000);
}
system("pause");
return 0;
}


编译时带上  -lpthread -lboost_thread

问题:

io_service_pool_.join(); // 为什么要join 这与单线程有什么区别?????????????

欢迎各位留言解答

你可能感兴趣的:(服务器开发)