我正在编写一个高效的套接字服务器。目的是良好的整体吞吐量。我使用主线程作为监听器。它async_accept
是一个客户端并将套接字添加到队列中。有一个调度程序线程从队列中拾取一个准备好从中读取的套接字,并将其添加到工作线程的队列之一。我保留了一个工作线程池。工作线程将执行实际的读/写。
我async_accept
在我的听众中使用。为了找出可以读取的套接字,我在调度程序中使用了 async_read_some。这个想法可行,但有一个问题。Myio_service.run()
在 listener 中被调用,所以在 dispatcher 中的 handler async_read_some
,实际上是在 listener 线程中运行的。
这是我的代码:
using boost::asio::ip::tcp;
using namespace std;
std::queue<std::shared_ptr<tcp::socket>> q_sock;
boost::mutex m_log1;
boost::condition_variable m_cond1;
boost::mutex::scoped_lock m_lock1 = boost::mutex::scoped_lock(m_log1);
sem_t _sem_sock;
enum { max_length1 = 1024 };
char data_1[max_length1];
void handle_read1(std::shared_ptr<tcp::socket> sock, const boost::system::error_code& error,
size_t bytes_transferred)
{
printf("handle_read1 : error : %s : %d, thread id is: %ld, pid : %d \n", error.category().name(), error.value(), (long int)syscall(SYS_gettid), getpid());
boost::asio::write(*(sock.get()), boost::asio::buffer(data_1, bytes_transferred));
}
void sock_dispatch() {
int v_size = 0;
std::shared_ptr<tcp::socket> curr_sock;
printf("sock_dispatch started. The ID of this of this thread is: %ld, pid : %d \n", (long int)syscall(SYS_gettid), getpid());
while(1) {
while(1) {
sem_wait(&_sem_sock);
v_size = q_sock.size();
sem_post(&_sem_sock);
if(v_size <= 0)
m_cond1.timed_wait(m_lock1,boost::posix_time::milliseconds(5000));
else
break;
}
sem_wait(&_sem_sock);
curr_sock = q_sock.front();
q_sock.pop();
sem_post(&_sem_sock);
curr_sock->async_read_some(boost::asio::buffer(data_1, max_length1),
boost::bind(handle_read1, curr_sock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
class session
{
public:
session(boost::asio::io_service& io_service)
: sockptr(new tcp::socket(io_service)) {}
void start()
{
printf("START NEW SESSION The ID of this of this thread is: %ld, pid : %d \n", (long int)syscall(SYS_gettid), getpid());
sem_wait(&_sem_sock);
q_sock.push(sockptr);
sem_post(&_sem_sock);
m_cond1.notify_all();
}
std::shared_ptr<tcp::socket> sockptr;
};
class server
{
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
session* new_session = new session(io_service_);
acceptor_.async_accept(*(new_session->sockptr.get()),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
printf("WAITING TO ACCEPT: The ID of this of this thread is: %ld, pid : %d \n", (long int)syscall(SYS_gettid), getpid());
}
void handle_accept(session* new_session,
const boost::system::error_code& error)
{
new_session->start();
new_session = new session(io_service_);
acceptor_.async_accept(*(new_session->sockptr.get()),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}
private:
boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
};
int main(int argc, char* argv[])
{
sem_init(&_sem_sock, 0, 1);
boost::asio::io_service io_service;
using namespace std;
server s(io_service, atoi(argv[1]));
boost::thread t(boost::bind(sock_dispatch));
io_service.run();
return 0;
}
此代码是从 boost::asio 示例http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp修改而来的。客户端代码是http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/example/echo/blocking_tcp_echo_client.cpp。
当客户端连接时,服务器的输出:
WAITING TO ACCEPT: The ID of this of this thread is: 3843, pid : 3843
sock_dispatch started. The ID of this of this thread is: 3844, pid : 3843
START NEW SESSION The ID of this of this thread is: 3843, pid : 3843
handle_read1 : error : system : 0, thread id is: 3843, pid : 3843
在这种情况下,调度程序线程 id 是 3944,但 handle_read1 在线程 3843 中运行。理想情况下,handle_read1 应该在调度程序中运行,因此它不会阻塞侦听器中的接受。
知道我应该怎么做才能实现这一目标吗?或者整个事情有更好的设计:)?