c++ - Getting iterators within TBB parallel_for loop -


i having issues conceptually straightforward code. need collect iterators vector inside loop, , use tbb capabilities parallelize loop. here minimal (not) working example:

#include <tbb/tbb.h>  #include <vector>  int main() {     std::vector<int> v{1,2,3,4,5,6,7,8};     tbb::concurrent_vector<decltype(v)::iterator> cv;     tbb::parallel_for(std::begin(v), std::end(v), [&](const auto& iter)     {         cv.emplace_back(iter);     }); } 

as can see, interested in iterators, not values. accomplish similar thing using pointers or reference wrappers (here example below), understand reason not able want...

#include <tbb/tbb.h>  #include <vector>  int main() {     std::vector<int> v{1,2,3,4,5,6,7,8};     tbb::concurrent_vector<std::reference_wrapper<decltype(v)::value_type>> cv;     tbb::parallel_for_each(std::begin(v), std::end(v), [&](auto& element)     {         cv.emplace_back(std::ref(element));     }); } 

you trying use following parallel_for overload:

template <typename index, typename function> void parallel_for(index first, index last, const function& f); 

this overload takes 2 indexes specify block range. index type must converted integers, in function body, i.e. lambda specified, can use v[index] access (index) elements of vector.

however, passed iterators overload , compiler couldn't convert these iterators integers, got error.

the solution follows:

tbb::parallel_for(static_cast<std::size_t>(0), v.size(), // use index specify range     [&](std::size_t index) {         cv.emplace_back(v.begin() + index);  // access element v[index]     }); 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -