假设我们有一个类:
class Foo
{
private:
Concurrency::task_group _tasks;
};
如何为该任务组分配调度程序?我不想使用默认调度程序,因为我还在代码中的其他地方使用parallel_for。
task_group 将通过调度程序设置其最大并发级别,以根据时间使用所有核心或其中的子集。该应用程序可能会运行几个小时,因此需要更改最大并发数。
我在 PPL 中找不到执行此操作的好方法。在 .NET 中,这非常简单 - 您所要做的就是设置 MaxDegreeOfParallelism。
有什么想法吗?
最佳答案
有几种解决方案:
如果您使用 MSVS C++ 2012,则:
_tasks.run([]() { Context::Oversubscribe(true); // // long time running task // Context::Oversubscribe(false); });
如果您的应用程序在 Win7x64 或 Windows Server 2008 R2 上运行,请尝试 UMS :
Scheduler::SetDefaultSchedulerPolicy( SchedulerPolicy(1, SchedulerKind, UmsThreadDefault) );
最后。在创建新的繁重任务之前,使用新的超额订阅策略(放大 MaxConcurrency)创建新的当前调度程序。
CurrentScheduler::Create( SchedulerPolicy(1, MaxConcurrency, 8) ); _tasks.run([](){ // // long time running task // }); CurrentScheduler::Detach();
关于task - VC++ PPL,将调度程序分配给task_group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13115440/