multithreading - Parallel implemention of Lisp-style mapping of a function to a list in C++ fails without cout after use of thread -


this code works when of lines under /* debug messages */ uncommented. or if list being mapped less 30 elements.

func_map linear implementation of lisp-style mapping , can assumed work.

use of follows func_map(func_ptr foo, std::vector* list, locs* start_and_end)

func_ptr pointer function returns void , takes in int pointer

for example: &foo in foo defined as:

void foo (int* num){ (*num) = (*num) * (*num);} 

locs struct 2 members int_start , int_end; use tell func_map elements should iterate over.

void par_map(func_ptr func_transform, std::vector<int>* vector_array) //function mapping function list alla lisp {     int array_size = (*vector_array).size(); //retain number of elements in our vector     int num_threads = std::thread::hardware_concurrency(); //figure out number of cores         int array_sub = array_size/num_threads; //number use figure out how many elements should assigned per thread      std::vector<std::thread> threads; //the vector initialize threads in     std::vector<locs> vector_locs; // vector store start , end position each thread      for(int = 0; < num_threads && < array_size; i++)     {         locs cur_loc; //the locs struct create using power of logic         if(array_sub == 0) //the logic         {             cur_loc.int_start = i; //if number of elements in array less number of cores assign 1 core each element         }         else         {             cur_loc.int_start = (i * array_sub); //otherwise figure out starting point given number of cores         }          if(i == (num_threads - 1))         {             cur_loc.int_end = array_size; //make sure elements iterated on         }         else if(array_sub == 0)         {             cur_loc.int_end = (i + 1); //ditto         }             else             {                 cur_loc.int_end = ((i+1) * array_sub); //otherwise use number of threads determine our ending point             }          vector_locs.push_back(cur_loc); //store created locs struct doesnt changed during reference         threads.push_back(std::thread(func_map,                                   func_transform,                                    vector_array,                                     (&vector_locs[i]))); //create thread          /*debug messages*/ // <--- whenever of these uncommented code works         //cout << "i = " << << endl;         //cout << "int_start == " << cur_loc.int_start << endl;         //cout << "int_end == " << cur_loc.int_end << endl << endl;         //cout << "thread " << << " initialized" << endl;       }      for(int = 0; < num_threads && < array_size; i++)     {         (threads[i]).join(); //make sure threads done     } } 

i think issue might in how vector_locs[i] used , how threads resolved. use of vector maintain state of locs instance referenced thread should prevent being issue; i'm stumped.

you're giving thread function pointer, &vector_locs[i], may become invalidated push_back more items vector.
since know beforehand how many items vector_locs contain - min(num_threads, array_size) - can reserve space in advance prevent reallocation.

as why doesn't crash if uncomment output, guess output slow thread started finish before output done, next iteration can't affect it.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -