问题描述
我有一个数字运算功能,所以我通过使用 PPL 并行了它..但是另一个开发人员由于某种原因要求这个功能串行运行..我需要给出一个参数,以便他可以调用我的函数在串行模式下......我不想复制代码所以我需要一种方法来限制 PPL 线程的数量..虽然我很伤心
I have a number crunching function, so I have paralleled it by using PPL..however another developer requires this function to be run in serial because of some reason..I need to give a parameter so that he can call my function in serial mode...I dont want to duplicate the code so I need a way to limit the number of PPL threads..Although I have sad
Concurrency::SchedulerPolicy sp( 1, Concurrency::MaxConcurrency, 1 );
CurrentScheduler::Create(sp);
PPL 创建两个线程并并行运行我的代码...任何关于如何序列化 ppl 增强代码的建议.
PPL creates two threads and running my code in parallel...Any suggestions how to serialize a ppl enhanced code.
推荐答案
对于这个问题最好不要设置调度策略,而使用一些手动的任务组初始化控制,例如:
For this problem better not set scheduler policies, and use some manual task group initialization control, for example:
using namespace Concurrency;
std::vector< task_handle< std::function< void() > > > aTask;
aTask.push_back( make_task([](){ /*taks 1*/}) );
aTask.push_back( make_task([](){ /*taks 2*/}) );
aTask.push_back( make_task([](){ /*taks 3*/}) );
task_group tGroup;
bool bSerialMode = true; /* or false */
if (!bSerialMode)
{
std::for_each(aTask.begin(), aTask.end(), [&](task_handle< std::function< void() > >& handle){
tGroup.run( handle );
});
}
else
{
tGroup.run( [&](){
std::for_each(aTask.begin(), aTask.end(), [&](task_handle< std::function< void() > >& handle){
tGroup.run_and_wait( handle ); });
});
}
如果您决定限制一个虚拟处理器的所有任务,那么也要设置 MinConcurrency.
If you do decide to limit all tasks of one virtual processor, then set MinConcurrency too.
CurrentScheduler::Create( SchedulerPolicy( 2, Concurrency::MinConcurrency, 1, Concurrency::MaxConcurrency, 1 ) );
这篇关于如何将 PPL 线程数设置为 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!