TParallel::For 默认使用多少个线程?

How many threads are used by default in TParallel::For?

我想在 C++ Builder 中使用 TParallel::For,但想知道在这个循环中默认使用多少线程?例如,如果我有 1000 次可以并行完成的迭代,它们是通过一次创建 1000 个线程来执行的,还是任何时候线程的最大数量受限于逻辑处理器的数量?

我的观点是 - 我不想让 TParallel::For 一次创建更多线程然后 CPU 支持。那么,如果 CPU 支持 8 个线程,那么这些迭代将以 8 乘 8 的速度执行,直到执行完所有 1000 次迭代,还是会同时创建 1000 个线程?

默认池处理程序查找计算机资源和计算机负载。

来自 docs(强调我的):

The RTL provides the Parallel Programming Library (PPL), giving your applications the ability to have tasks running in parallel taking advantage of working across multiple CPU devices and computers. The PPL includes a number of advanced features for running tasks, joining tasks, waiting on groups of tasks, etc. to process. For all this, there is a thread pool that self tunes itself automatically (based on the load on the CPU’s) so you do not have to care about creating or managing threads for this purpose.

如果你想控制池化,你可以define your own pool:

When APool parameter of type TThreadPool is specified, the program author controls the thread resources available to the iterated events of TParallel.For through the use of its TThreadPool.SetMinWorkerThreads and TThreadPool.SetMaxWorkerThreads methods. Be advised when calling these methods that too many simultaneous threads can generate overhead that diminishes or eliminates the benefit of executing routines in parallel.


默认线程池初始化有以下限制:

FMinLimitWorkerThreadCount := TThread.ProcessorCount;
FMaxLimitWorkerThreadCount := TThread.ProcessorCount * MaxThreadsPerCPU;

MaxThreadsPerCPU 是设置为 25 的常量。因此,根据处理器负载,池中使用的线程在这些数字之间变化。