当从线程池调用时,boost 的io_service是否共享请求的线程?
Is boost's io_service share threads for request when called from thread pool?
我的问题是关于提升asio的io_service。当我用该方法调用它时:
int main()
{
try
{
boost::asio::io_service io_service;
Server server(io_service);
std::vector<boost::shared_ptr<boost::thread> > threads;
for (std::size_t i = 0; i < 16; ++i)
{
boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&boost::asio::io_service::run, &io_service)));
threads.push_back(thread);
}
for (std::size_t i = 0; i < threads.size(); ++i)
threads[i]->join();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
它会动态共享请求的线程,还是只为连接组提供一个线程?谢谢。
asio::io_service
有一个共享事件队列。这些事件由当前调用 io_service::run() 的线程处理。所以是的,可能会从不同的线程调用处理程序。
我不建议您为服务器运行 16 个线程,因为它会降低您的速度(由于上下文切换和 boost::asio 瓶颈)。如果你真的需要这么多线程,那么更喜欢使用"每个线程io_service"的习惯用法。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- 为什么我不能在 while 循环中创建线程?