Boost Asio 多线程 TCP 同步服务器
我正在尝试创建一个 tcp 同步服务器。我的主线程将创建侦听端口,并且传入连接将由线程处理。
我的代码:
void WorkerThread(boost::shared_ptr< boost::asio::io_service > io_service)
{
io_service->run();
}
void Application::server()
{
boost::shared_ptr< boost::asio::io_service > io(
new boost::asio::io_service()
);
boost::shared_ptr< boost::asio::io_service::work > work(
new boost::asio::io_service::work(*io)
);
// Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR
boost::asio::ip::tcp::acceptor acceptor(*io);
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 2198);
acceptor.open(endpoint.protocol());
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor.bind(endpoint);
acceptor.listen();
// pool of threads
boost::thread_group worker_threads;
for(int x = 0; x < 5; ++x)
{
worker_threads.create_thread(boost::bind(&WorkerThread, io));
}
while(true)
{
boost::shared_ptr< boost::asio::ip::tcp::socket > socket(
new boost::asio::ip::tcp::socket( *io )
);
acceptor.accept(*socket);
processConnection(*socket);
socket->close();
}
io->stop();
worker_threads.join_all();
}
void Application::processConnection(boost::asio::ip::tcp::socket & socket)
{
boost::asio::streambuf request_buffer;
std::istream request_stream(&request_buffer);
// repsonse buffer
boost::asio::streambuf response_buffer;
std::ostream response_stream(&response_buffer);
boost::asio::read_until(socket, request_buffer, "</message>");
// process request_buffer into response_buffer
boost::asio::write(socket, response_buffer);
}
以下是与连接到服务器的多个客户端一起使用的;但是,如果我删除线程池,它也可以工作。谁能解释一下这是为什么吗?我还需要一个线程池吗?
I am trying to create a tcp synchronous server. My main thread would create listen to a port, and an incoming connection would be handled by a thread.
My code:
void WorkerThread(boost::shared_ptr< boost::asio::io_service > io_service)
{
io_service->run();
}
void Application::server()
{
boost::shared_ptr< boost::asio::io_service > io(
new boost::asio::io_service()
);
boost::shared_ptr< boost::asio::io_service::work > work(
new boost::asio::io_service::work(*io)
);
// Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR
boost::asio::ip::tcp::acceptor acceptor(*io);
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 2198);
acceptor.open(endpoint.protocol());
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor.bind(endpoint);
acceptor.listen();
// pool of threads
boost::thread_group worker_threads;
for(int x = 0; x < 5; ++x)
{
worker_threads.create_thread(boost::bind(&WorkerThread, io));
}
while(true)
{
boost::shared_ptr< boost::asio::ip::tcp::socket > socket(
new boost::asio::ip::tcp::socket( *io )
);
acceptor.accept(*socket);
processConnection(*socket);
socket->close();
}
io->stop();
worker_threads.join_all();
}
void Application::processConnection(boost::asio::ip::tcp::socket & socket)
{
boost::asio::streambuf request_buffer;
std::istream request_stream(&request_buffer);
// repsonse buffer
boost::asio::streambuf response_buffer;
std::ostream response_stream(&response_buffer);
boost::asio::read_until(socket, request_buffer, "</message>");
// process request_buffer into response_buffer
boost::asio::write(socket, response_buffer);
}
The following is working with more than one client connecting to the server; however, it also work if I remove the pool of thread. Can anyone explain me why that is? Do I even need a pool of threads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给定示例代码,您不需要线程池。无需在您的上下文中调用 io_service::run() ,请参阅 文档
您尚未向
io_service
添加任何处理程序,因此无需调用run()
。如果您使用异步方法,例如async_accept( )
,那么您需要run()
io_service
。You do not need a pool of threads given your sample code. There is no need to invoke
io_service::run()
in your context, see the documentationYou haven't added any handlers to the
io_service
so there is no need to invokerun()
. If you use asynchronous methods such asasync_accept()
, then you will need torun()
theio_service
.你可能会发现它更容易阅读你的代码,
如果你输入 boost stuff example,
这不会直接帮助,但它会有所帮助
you might find it easier to read your code if you typedef the boost stuff
example
This does not directly help but it will help