#include <tbb/task_scheduler_init.h>
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
#include <iostream> class ApplyFoo
{ private:
float *const my_a;
public:
void operator()(const tbb::blocked_range<size_t>&r)const
{
float *a=my_a;
for(int i=r.begin();i!=r.end();++i)
{
printf("gevar = %f\n",a[i]);
}
}
ApplyFoo(float a[]):
my_a(a)
{
}
};
void parallelApplyFoo(float a[],size_t n)
{
tbb::parallel_for ( tbb::blocked_range<size_t> (,n),ApplyFoo(a) ,tbb::auto_partitioner());
}
int main()
{
//tbb::task_scheduler_init init;
float a[]={,,,,,,,,,};
parallelApplyFoo(a,);
return ;
}

修改简化版本:

#include <iostream>
#include <tbb/task_scheduler_init.h>
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
#include <vector> using namespace std;
using namespace tbb; #define THREAD_FUNCTION void operator()(const blocked_range<size_t> &r)const
#define THREAD_EXCUTED_FOR(N,CLASS)\
{\
parallel_for(blocked_range<size_t>(,N),CLASS,auto_partitioner());\
} class ApplyFoo
{
public:
ApplyFoo()
{
}
void setData(float *data)
{
my_a = data;
}
THREAD_FUNCTION
{
//cout << "going to thread\n";
float *a = my_a;
for(size_t i=r.begin();i!=r.end();++i)
{
//cout << "going to thread:"<<i<<endl;
a[i] = ;
}
}
private:
float * my_a;
}; int main() { long int size = ;
vector<float> b(size);
cout << "loop size " << b.size() << endl;
cout << "start threading\n";
ApplyFoo foo;
foo.setData(b.data());
THREAD_EXCUTED_FOR(size,foo);
cout << "thread executed end\n";
cout << b[] << endl;
return ;
}
04-29 21:07