PPL将调度程序分配给task_group成员变量 [英] PPL assigning a Scheduler to a task_group member variable
问题描述
我实际上对此有困难.
Foo类具有私有成员"task_group _tasks" ...如何将调度程序正确地分配给不是默认调度程序的task_group?
class Foo has a private member "task_group _tasks"... how do I properly assign a scheduler to this task_group that is NOT the default scheduler?
我的情况是我想将"maxconcurrency"设置为取决于该任务是一天中的什么时间.我有让我一天中的时间都可用的代码,例如,如果时间不是正午,我希望使用除一个逻辑核以外的所有逻辑核.
My scenario is I would like to set "maxconcurrency" to this task_group depending on what time of the day it is. I have code that gets me the hour of the day, and if the time is less than noon, for instance, I would like to use all but one logical core.
我想保留在类中其他地方使用的parallel_for循环的默认调度程序.
I want to keep the default scheduler for the parallel_for loops I use elsewhere in the class.
任何帮助将不胜感激!
推荐答案
当您将调度程序附加到当前上下文时,调度的任务将在其上执行.请参见以下代码:
When you attach a scheduler to the current context, scheduled tasks will execute on it. Please see this code:
Concurrency::task_group tg; // TG is now associated with the current scheduler
tg.run(
[]()
{
printf( "task[1] CurrentScheduler = %d\r\n", Concurrency::CurrentScheduler::Id() ); // default scheduler
});
Concurrency::CurrentScheduler::Create(
Concurrency::SchedulerPolicy( 2,
Concurrency::MaxConcurrency, 2,
Concurrency::MinConcurrency, 2 ) );
tg.run(
[]()
{
printf( "task[2] CurrentScheduler = %d\r\n", Concurrency::CurrentScheduler::Id() ); // new scheduler
});
Concurrency::CurrentScheduler::Detach();
tg.run_and_wait(
[]()
{
printf( "task[3] CurrentScheduler = %d\r\n", Concurrency::CurrentScheduler::Id() ); // default scheduler
});
这篇关于PPL将调度程序分配给task_group成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!