from:
//有幾個地方改了下,要不編譯不過
#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
}void start()
}void join() }
void stop()
}boost::asio::io_service& get_io_service()
return io_service;
}private:
typedef boost::shared_ptrio_service_sptr;
typedef boost::shared_ptrwork_sptr;
typedef boost::shared_ptrthread_sptr;
boost::mutex mtx;
std::vectorio_services_;
std::vectorwork_;
std::vectorthreads_;
std::size_t next_io_service_;
};boost::mutex cout_mtx;
int packet_size = 0;
enum ;
class session
virtual ~session()
tcp::socket& socket()
inline void start()
void handle_read(const boost::system::error_code& error, size_t bytes_transferred)
boost::asio::async_write(socket_, boost::asio::buffer(data_, bytes_transferred),
boost::bind(&session::handle_write, this, boost::asio::placeholders::error));
}else
}void handle_write(const boost::system::error_code& error)
else
}private:
tcp::socket socket_;
char data_[max_packet_len];
int recv_times;
};class server
void handle_accept(session* new_session, const boost::system::error_code& error)
else
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()
private:
io_service_pool io_service_pool_;
tcp::acceptor acceptor_;
};int main()
system("pause");
return 0;
}編譯時帶上 -lpthread -lboost_thread
問題:io_service_pool_.join(); // 為什麼要join 這與單執行緒有什麼區別?????????????
boost高併發網路框架 執行緒池
boost的官方例子,有單執行緒的網路框架,httpserver2是執行緒池的。下面參照網上某人的 修改了一點 忘了哪位大仙的 了 測試工具,適用stressmark,測試效果非常好,9000個 s include include authenhandle.h include configure.h...
boost高併發網路框架 執行緒池
boost的官方例子,有單執行緒的網路框架,httpserver2是執行緒池的。下面參照網上某人的 修改了一點 忘了哪位大仙的 了 測試工具,適用stressmark,測試效果非常好,9000個 s include include authenhandle.h include configure.h...
Quartz定時任務框架執行緒池
springboot使用 enablescheduling註解開啟定時任務功能後,預設建立乙個固定執行緒數為1的執行緒池給定時任務框架呼叫執行定時任務。當多個任務同時執行時,會導致多個任務競爭執行執行緒,當上乙個任務執行完畢後,執行緒才會被釋放出來用於其他任務的執行。導致定時任務延時執行。因此需要建...