Vibhor
Vibhor

Reputation: 45

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

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?

Upvotes: 2

Views: 578

Answers (2)

Anton
Anton

Reputation: 6537

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.

Upvotes: 7

schorsch312
schorsch312

Reputation: 5694

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

Upvotes: 1

Related Questions