Boost Asio,多个线程和多个io_service [英] Boost Asio, Multiple threads and multiple io_service

查看:867
本文介绍了Boost Asio,多个线程和多个io_service的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个使用多个线程和多个io_service实例(每个线程使用一个io_service实例)的服务器来侦听端口.对于每个io_service实例,我创建一个相应的boost :: asio :: ip :: tcp :: acceptor对象.我尝试以下代码:

I want to develop a server use multiple threads and multiple io_service instance(each thread use an io_service instance) to listen at a port. For each io_service instance I create a corresponding boost::asio::ip::tcp::acceptor object. I try this code:

 using namespace boost::asio;
    ...
    io_service io_service_;
    io_service io_service_1;

    ip::tcp::acceptor* acceptor_;
    acceptor_ = new ip::tcp::acceptor(io_service_);
    ip::tcp::endpoint ep( ip::tcp::v4(), LISTEN_PORT);
    acceptor_->open(ep.protocol());
    acceptor_->bind(ep);
    acceptor_->listen();

    ip::tcp::acceptor* acceptor_1;
    acceptor_1 = new ip::tcp::acceptor(io_service_1);
    acceptor_1->open(ep.protocol());
    acceptor_1->bind(ep);
    acceptor_1->listen();
    ...

    boost::thread th( boost::bind(&io_service::run, &io_service_));
    boost::thread th( boost::bind(&io_service::run, &io_service_1));
    ...

在运行时将显示错误对话框:

when running will display error dialog:

boost :: exception_detail :: clone_impl< boost :: exception_detail :: error_info_injector>在内存位置0x001FF704.

boost::exception_detail::clone_impl< boost::exception_detail::error_info_injector > at memory location 0x001FF704.

您能帮助我如何使服务器具有多个线程,每个线程都使用io_service实例吗?

can you help me how to make a server with Multiple threads, each thread use an io_service instance?

更新:正如我在Boost.Asio C ++网络编程中所读到的,有3种方法可以将io_service与线程一起使用:

Update: as I read in Boost.Asio C++ Network Programming, have 3 way to use io_service with thread:

  1. 具有一个io_service和一个处理程序线程的单线程(运行io_service :: run()的线程)
  2. 具有单个io_service实例和多个io_service实例的多线程 处理程序线程
  3. 具有多个io_service实例和多个线程的多线程
  1. Single-thread with one io_service and one handler thread(thread running io_service::run())
  2. Multi-threaded with a single io_service instance and several handler threads
  3. Multi-threaded with several io_service instances and several threads

我可以实现情况1和2.但是对于情况3,我不知道如何实现它来处理许多并发连接,我应该使用1个线程来处理1个io_service(如上所述)吗?情况3是否比情况2有更好的性能(可以处理更多的并发连接)?

I can implement case 1 and 2. But with case 3, I don't know how to implement it to handle many concurrent connections, should I use 1 thread to handle 1 io_service(as above)? Is case 3 has better performance(can handle more concurrent connections) than case 2?

推荐答案

您可以使用多个线程,但需要为端口使用1个接受器.

You can use multiple threads, but you need to use 1 acceptor for a port.

IO服务是线程安全的,因此您可以在多个线程上使用一个服务.

IO services are thread-safe, so you can use one service on many threads.

您甚至可以拥有多个io服务,没问题.但是,您不能将多个接受者绑定到同一端口(也许除非绑定到不同的逻辑网络接口,但这并不是真正的同一端口".)

You can even have multiple io services, no problem. You can't bind multiple acceptors to the same port though (unless you bind to different logical network interfaces, perhaps, but that's not really "the same port" then).

#include <boost/asio.hpp>
#include <boost/thread.hpp>

using namespace boost::asio;
using namespace boost;

int main() {
    io_service io_service_;

    ip::tcp::acceptor acceptor_(io_service_, { {}, 6767 });
    acceptor_.bind({ ip::tcp::v4(), 6767 });

    boost::thread_group tg;
    for (unsigned i = 0; i < thread::hardware_concurrency(); ++i)
        tg.create_thread(boost::bind(&io_service::run, &io_service_));

    // ...
    //
    tg.join_all();
}

这篇关于Boost Asio,多个线程和多个io_service的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆