我应该创建多少个线程? [英] How many threads should I create?

查看:171
本文介绍了我应该创建多少个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个问题,我有一个类,其构造函数只有一些分配,然后有一个 build()成员函数实际上做的工作。

Based on this question, I have a class, where its constructor does only some assignments and then there is a build() member function which actually does the job.

我知道我将要构建的对象的数量在[2,16]的范围内。实际的数字是一个用户参数。

I know that the number of objects I will have to build is in the range of [2, 16]. The actual number is a user parameter.

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

I create my objects in a for loop like this

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

然后在另一个for循环中创建线程。每个线程基于以下逻辑调用一组对象中的 build()

and then in another for loop I create the threads. Every thread calls build() in a chunk of objects, based on this logic:


如果你的向量有n个元素,你有p个线程,
线程只写给元素

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

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

[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 。在处理 C ++ 11功能所具有的一些问题之后,我看到了以下内容:

where n is the number of objects I want to create and P the return value of std::thread::hardware_concurrency. After dealing with some issues that C++11 feature has, I read this:


即使实现了hardware_concurrency,也不能将其直接映射到核心数。这是标准说的返回 - 硬件线程上下文的数量。并继续到状态 - 这个值应该只被认为是一个提示如果你的机器启用了超线程,完全可能的返回值是核心数量的2倍。如果你想要一个可靠的答案,你需要使用操作系统提供的任何设施。 - Praetorian

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

这意味着我应该改变方法,因为这段代码是要从几个用户执行只有在我的系统中,很多人都会去运行那段代码)。所以,我想选择线程的数量,将标准和高效的方式。由于对象的数量相对较少,有一些规则要遵循或者有什么?

That means that I should probably change approach, since this code is meant to be executed from several users (and I mean not only in my system, many people are going to run that code). So, I would like to choose the number of threads in a way that will be both standard and efficient. Since the number of objects is relatively small, is there some rule to follow or something?

推荐答案

code> hardware_concurrency 线程并按先来先服务的顺序排列项目。

Just pick a thread pool of hardware_concurrency threads and queue the items on a first come, first served basis.

如果系统中的其他进程优先级从操作系统,所以是这样。这仅仅意味着小于分配的池大小(例如 P-1 )可以同时运行。没关系,因为第一个可用的池线程被完成 build() - 一个项目将从队列中选择下一个项目。

If other processes in the system somehow get priority from the OS, so be it. This simply means that fewer than the allocated pool size (e.g. P - 1) can run simultaneously. It doesn't matter since the first available pool thread that is done build()-ing one item will pick the next item from the queue.

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

To really avoid threads competing over the same core, you could


  • 使用信号量

  • use a semaphore (interprocess semaphore if you want to actually coordinate the builder threads from separate processes)

线程关联性(以防止操作系统将特定线程调度到不同的核心上)时间片);不幸的是,我不认为有标准,平台无关,设置线程亲和力的方式。

thread affinity (to prevent the OS from scheduling a particular thread onto a different core the next time slice); sadly I don't think there is standard, platform-independent, way to set thread affinity (yet).

我没有看到任何令人信服的理由使它更复杂

I see no compelling reason to make it more complicated

这篇关于我应该创建多少个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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