在具有许多内核的计算机上,使用 Boost ASIO 只能使用 1 个线程

only 1 thread possible with boost asio on machines with many cores

本文关键字:ASIO 线程 Boost 使用 许多 内核 计算机      更新时间:2023-10-16

我们遇到了非常奇怪的问题,我们设法创建了非常小的可重现(在某些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<>对象。

现在看看你的代码。