TBB:如何在代码的不同部分设置不同数量的线程?
TBB: How to set different number of threads in different parts of code?
我有一个tbb::task_scheduler_init
实例init
它用我无法控制的代码部分中的某些一定数量的线程初始化,在后面的代码部分中,我想使用不同数量的线程运行。如何通过此实例init
执行此操作?
有没有比执行以下操作更好的方法?
init.terminate();
init.initialize(my_preferred_number_of_threads);
/*
run some code
*/
init.terminate();
init.initialize(original_number_of_threads); // restore the original tbb scheduler
您可以使用 tbb::task_arena 来满足自己的需求。
摘自 tbb::task_arena 文档
tbb::task_scheduler_init def_init; // Use the default number of threads.
tbb::task_arena limited(2); // No more than 2 threads in this arena.
tbb::task_group tg;
limited.execute([&]{ // Use at most 2 threads for this job.
tg.run([]{ // run in task group
tbb::parallel_for(1, N, unscalable_work());
});
});
// Run another job concurrently with the loop above.
// It can use up to the default number of threads.
tbb::parallel_for(1, M, scalable_work());
// Wait for completion of the task group in the limited arena.
limited.execute([&]{ tg.wait(); });
相关文章:
- 将更高的优先级设置为 boost::asio 线程处理进程
- 是否可以在单独的线程中将 QObject 设置为 QML 上下文属性?
- 如何使用 pthreads 以正确的方式设置两个线程之一的优先级
- 设置 JVM 通过 JNI 初始化时创建的线程数
- 设置提升记录器的线程名称
- 如何在 OpenMP 中设置线程数
- OpenBLAS 只为一个例程设置线程数
- 在 OpenCV 上设置线程相关性
- 如何在 Qt5 中设置线程的 CPU 关联?
- 检查Linux功能以设置线程优先级
- std::c++ 中的线程在设置线程函数时不会出错
- 如何在C++中设置线程数
- 如何在Mac操作系统上设置线程优先级pthreads
- 在 Python 和 Java 中设置线程亲和力
- 从正在运行的进程设置线程优先级
- 需要为Solaris 10上的GNU g++2.95.3设置线程安全的std::string
- Windows设置线程关联掩码无效
- 如何在log4cplus中设置线程名称?
- 从创建线程设置线程的"nice level"?
- 在Qt中设置线程中的小部件标签文本