I have a tbb::task_scheduler_init
instance init
which is initialized with some certain number of threads in a part of code which I don't have control, in later parts of code I want to run with different number of threads. How can I do it through this instance init
?
Is there a better way than doing the following?
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
You can use tbb::task_arena for your needs.
Taken from tbb::task_arena documentation
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(); });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.