tbb parallel_for example c++ without lambda

萝らか妹 提交于 2019-12-06 04:41:48

parallel_for will take any functor, which can be a lambda, a functor class or a plain old function; the following should work just fine too:

#include "tbb/tbb.h"
using namespace tbb;
...
void print( size_t n) {
   printf("hellow world %d\n", n);
}
void print_range( const blocked_range<size_t> & r ){
     for( size_t i = r.begin(); i != r.end(); ++i )
         printf("hello from range: %d\n", i);
}
void doit() {
      parallel_for<size_t>( 1, 10, 1, print );
      parallel_for( blocked_range<size_t>(1,10), print_range );
}

To use lambdas download gcc version 4.7 or later and give him option -std=c++11

#include "tbb/tbb.h"
using namespace tbb;
class ApplyFoo {
  float *const my_a;
  public:
    void operator()( const blocked_range<size_t>& r ) const {
      float *a = my_a;
      for( size_t i=r.begin(); i!=r.end(); ++i )
        Foo(a[i]);
    }
    ApplyFoo( float a[] ) :
      my_a(a) {}
  };
  void ParallelApplyFoo( float a[], size_t n ) {
  parallel_for(blocked_range<size_t>(0,n), ApplyFoo(a));
}

Source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!