英特尔tbb parallel_for:通过类成员函数传递参数吗? [英] Intel tbb parallel_for: pass class member function with parameters?
本文介绍了英特尔tbb parallel_for:通过类成员函数传递参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将类成员函数(带有参数)传递给parallel_for?大致情况:
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> )
}
推荐答案
查看他们创建一个类,并提供具有所有所需参数的类.
在该类中还有一个operator()
,它对数据进行运算.
然后使用该类的实例调用parallel_for.
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
您如何做:
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));
这篇关于英特尔tbb parallel_for:通过类成员函数传递参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文