c++ - PPL:线程池的初始化

标签 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;
}

最佳答案

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

关于c++ - PPL:线程池的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18125072/

相关文章:

c++ - boost::threads 示例和堆损坏消息

c++ - 在 ";"错误之前继续丢失 "."

c++ - 在俄语本地化的 Visual Studio 中构建时,编译器警告是乱码

asp.net-mvc - 如何在 Visual Studio 2012 或 Visual Studio 2013 中打开旧的 MVC 项目?

.net - 添加自定义 xmlns 后 Xaml 智能感知不起作用

c++ - C 函数 'void msgBox(const char*, const char*)' 的声明与之前的声明冲突

c++ - 如何在监 window 口中转换为未命名命名空间中的类型?

c++ - 导出静态函数

c++ - 在 OpenGL 中的一个窗口上显示两个不同的图像

C++ 模板类型检查 std::is_same 不起作用?