在具有许多内核的计算机上,使用 Boost ASIO 只能使用 1 个线程
only 1 thread possible with boost asio on machines with many cores
我们遇到了非常奇怪的问题,我们设法创建了非常小的可重现(在某些PC上)示例: 主.cpp Dockerfile 这段代码绝对没有任何用处,它只记录几行,但这几行证明它不适用于超过 1 个线程。
我们使用助推器 1.70 (Asio 1.14.0)
到目前为止,我在 3 个计算上进行了测试:
我的桌面锐龙 1950X 64 GB RAM 大多最终具有:
started for 1 threads
Listener started on thread: 140593064609536
started for 2 threads
started for 3 threads
started for 4 threads
started for 5 threads
started for 6 threads
started for 7 threads
(有时 2 或 3 也有效,但大多数无效) 我在这台机器上也使用 Windows 上的 msvc 构建进行了测试,结果是它运行良好,因此问题以某种方式特定于 linux+core 计数。
主服务器 2x E5-2630 v3 128 GB RAM 大多最终具有:
root@cf8c892390ce:/app/test/bin# ./test
started for 1 threads
Listener started on thread: 140062574507776
started for 2 threads
started for 3 threads
started for 4 threads
started for 5 threads
started for 6 threads
started for 7 threads
(在 100 多次测试中一次 2 也有效,但从未更多)
旧测试服务器 2x 旧英特尔 2 核 CPU 4 GB 内存 大多数结果如下所示:
root@f06821a4cbc8:/app/test/bin# ./test
started for 1 threads
Listener started on thread: 140650646316800
started for 2 threads
Listener started on thread: 140650621138688
started for 3 threads
Listener started on thread: 140650246829824
started for 4 threads
Listener started on thread: 140650213259008
started for 5 threads
Listener started on thread: 140649944823552
started for 6 threads
Listener started on thread: 140649743496960
started for 7 threads
Listener started on thread: 140649726711552
(有时 5、6 或 7 个线程不起作用)
我们在其他几个系统上进行了测试,只有 1 个线程可靠地工作。
你能看看代码,告诉我我们在那里是否有一些愚蠢的错误?,或者这是否是asio中的错误?
最重要的是,我们可以以某种方式修复它吗?
您可能会在发布实际工作之前启动事件循环(run() 和相关)。
这将允许服务在侦听器启动之前完成(争用条件),这解释了症状。
避免这种情况的常用方法是使用work<>
对象。
现在看看你的代码。
相关文章:
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 提升 ASIO - io_service 不要等待连接到线程
- ASIO signal_set多个 IO 线程不可靠,具体取决于代码顺序?
- 将更高的优先级设置为 boost::asio 线程处理进程
- asio 链对象线程安全吗?
- asio::io_service 具有多个线程的优先级队列处理
- boost信号和插槽在不同的线程中不工作(使用boost::asio::io_service)
- 调用boost.asio的异步函数时,线程是什么时候创建的
- 调用socket.remote_endpoint(提升 asio 库)线程安全性
- 使用线程池提升 ASIO 多线程 TCP 服务器
- 重写多线程事件驱动的C 程序以使用单线程Boost :: Asio
- 在具有许多内核的计算机上,使用 Boost ASIO 只能使用 1 个线程
- 如何将每线程用户数据传递到ASIO处理程序中
- 提高 ASIO stream_descriptor和事件 FD 线程安全性
- 提升 asio io_service多线程性能不佳
- 提升:偶尔同步任务的ASIO线程池实现
- 如何在 boost::asio 线程池中使任务可取消/可中断
- c++ Boost::ASIO线程池问题
- 带线程超时的Boost ASIO线程池
- boost::asio线程池与io_service_per_cpu设计