C++ basic multithreading troubleshooting -


whenever try compile program, error: "error c2661: 'std::thread::thread' : no overloaded function takes 7 arguments". when try make threads

genparts<t> 

or

genparts. 

i'm trying familiarize myself threads @ moment, it's worth.

i know shouldn't have function many parameters passed in, , better pass in struct information-- i'll fix once i've got , running.

template <typename t> void genparts(unsigned int target, unsigned int &total, unsigned int low, unsigned int high, map<unsigned int, unsigned int> container, t gen){ return;} 

and calling function:

void genpart(unsigned int target, map<unsigned int, unsigned int> &container, t& gen){ unsigned int total[2]; map<unsigned int, unsigned int> results[2]; do{     total[0]=0;     total[1]=0;     thread t1(genparts<t>, target, total[0], 1, target/2, results[0], gen);     thread t2(genparts<t>, target, total[1], 1+(target/2), target, results[1], gen);     t1.join();     t2.join(); }while(total[0]+total[1] != target); } 

i appreciate help! thanks!

lots of details wrong... number of arguments you're passing along (where did container go?), , you're making copies of map time. importantly, arguments need passed reference thread function need wrapped in std::ref. putting together, here's version "works":

#include <map> #include <thread>  template <typename t> void genparts(unsigned int target, unsigned int &total,               unsigned int low, unsigned int high,               std::map<unsigned int, unsigned int>& container, t& gen) { /* ... */   }  template <typename t> void genpart(unsigned int target,              std::map<unsigned int, unsigned int> &container, t& gen) {   unsigned int total[2];   std::map<unsigned int, unsigned int> results[2];     {     total[0] = 0;     total[1] = 0;     std::thread t1(genparts<t>, target, std::ref(total[0]), 1,                    target / 2, std::ref(container), std::ref(gen));     std::thread t2(genparts<t>, target, std::ref(total[1]), 1 + (target / 2),                    target, std::ref(container), std::ref(gen));     t1.join();     t2.join();   } while (total[0] + total[1] != target); } 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -