multithreading - Creating threads to process multiple objects C++ -


first of let me you're great community. i've visited website hundreds of times , got help!

but still, quite new @ programming , there can't find answer in internet, going ask here see if can help. also, english not apology in advance, hope guys understand question:

i need multithread program in c++ create maximum 5 threads process std::vector 100 objects within it. here question: how should create loops? in order create 5 threads, each 1 process single object. when thread finishes can continue next one.

void * threaded_objectprocessor (void *voidarg) { //process object }   main() { //beginning of code       (std::vector<myobject>::iterator = myvector.begin(); != myvector.end(); it++)     {         //don't know here     }     //rest of code  } 

i hope explained myself enough, , thank guys in advance, welcome.

antirreni91

simple solution use openmp library. needs -fopenmp compiler flag , application must linked libgomp library. code need modifications:

for (std::vector<myobject>::iterator = myvector.begin(); != myvector.end(); it++) {     //don't know here } 

must changed definitive-end-condition compiler. after changes code this:

const size_t count = myvector.size(); #pragma omp parallel num_threads(5) (size_t = 0; < count; ++i) {     // or change how want work vector     threaded_objectprocessor(myvector[i]); } 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -