问题
Is it possible to pass a class member function (with parameters) to parallel_for? Something along the lines:
void classT::A(const tbb::blocked_range<std::size_t>& r,b) {}
void classT::B(e,f,g) {
tbb::parallel_for( blocked_range<size_t>(0,n), <need to call A with parameter b,
along with r> )
}
回答1:
Look at this example.
They create a class and suply the class with a all the parameters needed.
In that class is also a operator()
which does an oppertion on the data.
The parallel_for is then called with a instance of that class.
parallel_for(blocked_range<int>(0, nElements, 100), ArraySummer( p_A, p_B, p_SUM_TBB ) );
//The class is arraysummer
How you could do it :
class ClassTACaller
{
int* m_parameter;
ClassT* m_Tinstance
public:
ClassTACaller(ClassT* tinstance, int* param):m_parameter(param), m_Tinstance(tinstance){}
void operator() ( const blocked_range<int>& r ) const
{
m_Tinstance->A(r, param);
}
};
parallel_for(blocked_range<int>(0, nElements, 100), ClassTACaller(&classTinstance, &x));
来源:https://stackoverflow.com/questions/26689659/intel-tbb-parallel-for-pass-class-member-function-with-parameters