multithreading - Compilation error with c++11 thread -


i try understand why code gives compilation error. i've made minimal example keep same behaviour / architecture , error code in project. goal made parallel evaluation of object in vector.

template < class inputiterator, class outputiterator > void evaluate( inputiterator first, inputiterator last, outputiterator outfirst ) {     while( first != last ) {         *outfirst++ = (*first++) / 2 ; // example     } }  template < int num_t = 4 > struct parallelevaluator {      template< typename inputcontainer , typename outputcontainer >     static void eval(inputcontainer const & src, outputcontainer & dst) {         std::vector<std::thread> threads ;         auto bg = src.begin() ;         auto bg_r = dst.begin() ;         ( int = 0 ; < num_t ; i++ ) {             threads.push_back(std::thread(evaluate<typename inputcontainer::iterator,typename outputcontainer::iterator>                                           ,bg,bg+4,bg_r)) ;             bg = bg+4 ;             bg_r = bg_r + 4 ;         }         std::for_each(threads.begin(),threads.end(),std::mem_fn(&std::thread::join));     } };   int main() {     std::vector<int> t = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} ;     std::vector<int> v(16) ;      parallelevaluator<4>::eval(t,v) ;      return 0; } 

when try compile code following error :

/usr/include/c++/4.8/functional:1697: error: no type named 'type' in 'class std::result_of<void (*(__gnu_cxx::__normal_iterator<const int*, std::vector<int> >, __gnu_cxx::__normal_iterator<const int*, std::vector<int> >, __gnu_cxx::__normal_iterator<int*, std::vector<int> >))(__gnu_cxx::__normal_iterator<int*, std::vector<int> >, __gnu_cxx::__normal_iterator<int*, std::vector<int> >, __gnu_cxx::__normal_iterator<int*, std::vector<int> >)>' 

i saw kind of error occurs when reference using in thread callback not case here.

does know why error occurs ? need specific syntax use template , co ?

thanks

the type of bg not std::vector<int>::iterator, std::vector<int>::const_iterator since src const&. lazy , let compiler deduce correct types decltype. (also, avoid memory allocations using std::array<std::thread,num_t> instead of std::vector<std::thread>.):

template< typename inputcontainer , typename outputcontainer > static void eval(inputcontainer const & src, outputcontainer & dst) {     std::array<std::thread, num_t> threads ;     auto bg = src.begin() ;     auto bg_r = dst.begin() ;     ( int = 0 ; < num_t ; i++ ) {         threads[i] = std::thread(evaluate<decltype(bg),decltype(bg_r)>                                  ,bg,bg+4,bg_r)) ;         bg = bg+4 ;         bg_r = bg_r + 4 ;     }     std::for_each(threads.begin(),threads.end(),std::mem_fn(&std::thread::join)); } 

if don't find lambdas offensive, threads[i] = std::thread([=]{ evaluate(bg,bg+4,bg_r); }); same thing bit less syntax.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -