PPL:线程池的初始化

时间:2013-08-08 11:40:17

标签: c++ visual-c++ visual-studio-2012 tbb ppl

有预先初始化PPL线程池的标准方法吗?问题是:PPL在运行时创建其线程池,例如parallel_for()正在执行。由于创建了额外的线程,在第一次运行期间,这会花费一点性能。

为了澄清问题,这里有一个例子:

#pragma once

#include <vector>
#include <ppl.h>

using namespace std;
using namespace Concurrency;

// Use this define for experiments.
#define PPL_INIT_METHOD 2

int main()
{
#if (PPL_INIT_METHOD == 0)    // experiment 1: initialize default scheduler

  CurrentScheduler::Create(SchedulerPolicy());
  // After this call only one additional thread is created

#elif (PPL_INIT_METHOD == 1)  // experiment 2: initialize custom scheduler

  SchedulerPolicy my_policy(3,
                            MinConcurrency, 12,
                            MaxConcurrency, 12,
                            ContextPriority, THREAD_PRIORITY_NORMAL);
  Scheduler* my_scheduler = Scheduler::Create(my_policy);
  // After this call only one additional thread is created
  my_scheduler->Attach();
  assert(my_scheduler->GetNumberOfVirtualProcessors() == 12);
  // Still same number of threads (= 2)

  // cleanup stuff ...

#else      // experiment 3: execute dummy parallel_for()

  std::vector<int> v(1024*1024, 42);
  parallel_for(0u, v.size(), [&v](size_t i) { v[i] += 1; }, static_partitioner());
  // After this call all 12 threads are created and ready for more work!

#endif

  // Do real work now!!!

  return 0;
}

1 个答案:

答案 0 :(得分:0)

您可以在concrt.h中调用Concurrency :: Scheduler :: Create来初始化ppl线程池。