我使用std::for_each
with std::execution::par
来对表示为结构向量的巨大输入执行复杂的计算。该计算不需要任何与硬件有关的延迟(例如,网络或磁盘IO),它是“仅CPU”计算。对我来说,似乎没有道理可以创建更多拥有硬件的OS线程;但是,Visual C ++ 2019平均创建50个线程,有时甚至只有12个硬件线程,最多可以创建500个线程。
有没有办法来限制并行线程计数至hardware_concurrency
与std::for_each
和std::execution::par
,或创建合理的线程数是使用自定义代码的唯一途径std::thread
?
Is it possible to limit threads count for C++ 17 parallel
for_each
?
No, at least not in C++17.
However, there is a proposal for executors
in a standard to come, which basically gives you the ability to influence the execution context (in terms of location and time) for the high-level STL algorithm interface:
thread_pool pool{ std::thread::hardware_concurrency() }; auto exec = pool.executor(); std::for_each(std::execution::par.on(exec), begin(data), end(data), some_operation);
Up to then, you have to either trust your compiler vendor that he knows what is best for the overall performance, as e.g. the developers of Visual Studio state:
Scheduling in our implementation is handled by the Windows system thread pool. The thread pool takes advantage of information not available to the standard library, such as what other threads on the system are doing, what kernel resources threads are waiting for, and similar. It chooses when to create more threads, and when to terminate them. It’s also shared with other system components, including those not using C++.
The other option would be to give up on solely relying on the standard library and use STL implementations which already feature the new proposal.