我应该创建多少个线程?

How many threads should I create?

根据 问题,我有一个 class,它的构造函数只做一些赋值,然后有一个 build() 成员函数实际完成工作。

我知道我必须构建的对象数量在 [2, 16] 范围内。实际数量是用户参数。

我像这样在 for 循环中创建我的对象

for (int i = 0; i < n; ++i) {
  roots.push_back(RKD<DivisionSpace>(...));
}

然后在另一个 for 循环中创建线程。每个线程在一个对象块中调用 build(),基于这个逻辑:

If your vector has n elements and you have p threads, thread i writes only to elements

[i n / p, (i + 1) n / p).

那么比如情况是这样的:

std::vector<RKD<Foo>> foos;
// here is a for loop that pushes back 'n' objects to foos

// thread A         // thread B                 // thread C
foos[0].build();    foos[n / 3 + 0].build();    foos[2 * n / 3 + 0].build();
foos[1].build();    foos[n / 3 + 1].build();    foos[2 * n / 3 + 1].build();
foos[2].build();    foos[n / 3 + 2].build();    foos[2 * n / 3 + 2].build();
...                 ...                         ...

我采用的方法是确定线程数 p,如下所示:

p = min(n, P) 

其中 n 是我要创建的对象的数量,P std::thread::hardware_concurrency. After 的 return 值与 C++11 功能存在的一些问题,我读到这个:

Even when hardware_concurrency is implemented, it cannot be relied as a direct mapping to the number of cores. This is what the standard says it returns - The number of hardware thread contexts. And goes on to state - This value should only be considered to be a hint If your machine has hyperthreading enabled, it's entirely possible the value returned will be 2x the number of cores. If you want a reliable answer, you'll need to use whatever facilities your OS provides. – Praetorian

这意味着我可能应该改变方法,因为这段代码要由多个用户执行(我的意思是不仅在我的系统中,很多人都会 运行 该代码)。因此,我想以既标准又高效的方式选择线程数。由于对象数量比较少,请问有什么规律可循吗?

只需选择一个包含 hardware_concurrency 个线程的线程池,然后按照先到先得的原则对项目进行排队。

如果系统中的其他进程以某种方式获得 OS 的优先级,那就这样吧。这只是意味着少于分配的池大小(例如 P - 1)可以同时 运行。这无关紧要,因为第一个可用的池线程完成 build()-ing 一个项目将从队列中选择下一个项目。

要真正避免线程竞争同一核心,您可以

  • 使用信号量(如果您想实际协调来自不同进程的构建器线程,则使用进程间信号量)

  • 线程关联(以防止OS在下一个时间片将特定线程调度到不同的核心);遗憾的是,我不认为有标准、独立于平台的方法来设置线程亲和性(目前)。

我看不出有什么令人信服的理由让它变得更复杂