C++ How can I run three diffrent parallel_for functions at once apart from using tbb::task_group?

563 views Asked by At

I have code where I have to run parallel_for (independent from each other) at once parallely.

Code is something like:

tbb::parallel_for(range1,func1());//first

tbb::parallel_for(range2,func2());//second

tbb::parallel_for(range3,func3());//third

I have tried using task_group. Is there any other method available?

2

There are 2 answers

2
Anton On BEST ANSWER

There are many ways to run any parallel algorithm in parallel, you want just run it inside another parallel algorithm of your choice. task_group is just one example. The simplest approach for your case is to use parallel_invoke:

tbb::parallel_invoke([]{
        tbb::parallel_for(range1,func1);//first
    }, []{
        tbb::parallel_for(range2,func2);//second
    }, []{
        tbb::parallel_for(range3,func3);//third
    }
);

but one can choose to use another parallel_for over array of ranges and function pointers, or use parallel_pipeline, parallel_for_each, or raw low-level tbb::task.

3
schorsch312 On

You can put them each in a single std::thread and make a join afterwards. See also also.