c++ - Process vector elements in separate threads -


i'm trying process elements of vector in separate threads , put results in different vector. i've tried mutexes , critical sections around code check , take out elements input vector, i'm getting access violations when run code.

edit: i've updated code put results in vector in different critical section , initialize vector before threads start.

#include "stdafx.h" #include <windows.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> #include <process.h> #include <iostream> #include <vector>  #define max_threads  4  void invertproc( void * myid );      // threads 2 n: display  void shutdown( void );               // program shutdown   int     threadnr;                    // number of threads started  critical_section cs, cs2;  std::vector<int> _otempvector; std::vector<int> _ooutvector;  int outcounter;  int _tmain(int argc, _tchar* argv[]) {         threadnr = 0;    outcounter = 0;     ( int = 0; < 50000; i++ ) {       _otempvector.push_back( );       _ooutvector.push_back( 0 );    }     initializecriticalsection( &cs );       initializecriticalsection( &cs2 );      std::vector<handle> events;    ( threadnr = 0; threadnr < max_threads; threadnr++ ) {                   handle handle = (handle)_beginthread( invertproc, 0, &threadnr );             events.push_back( handle );    }     waitformultipleobjects( events.size(), &events[0], true, infinite );       std::cout << "outvector contains:" << _ooutvector.size() << "elements";     std::cout << '\n';  }   void invertproc( void *pmyid ) {       {       entercriticalsection( &cs );        if ( _otempvector.size() > 0 ) {          int ielement = _otempvector.back();          _otempvector.pop_back();          leavecriticalsection( &cs );           ielement *= -1;           entercriticalsection( &cs2 );           _ooutvector[outcounter] = ielement;           outcounter++;          leavecriticalsection( &cs2 );       }    } while ( _otempvector.size() > 0 );    } 

your output vector isn't inside critical section shared object should.... because if several thread try push_back @ same time, face write on write data race!!!!

  entercriticalsection( &cs );    if ( _otempvector.size() > 0 ) {      int ielement = _otempvector.back();      _otempvector.pop_back();           ielement *= -1;      _ooutvector.push_back( ielement );         leavecriticalsection( &cs );   }  } while ( _otempvector.size() > 0 );    

the resulting running thread run more sequentially concurrently

to solve that, must thing differently: 1) can split problem, each thread should work on consecutive element of input vector 2) if output vector initialize @ beginning size of input, remove problem due push!

each running thread *-1 given range of number input put result @ specific place of output vector (without interaction) other thread can remove lock

void invertproc( void *pmyid ) {        int threadnum = *((int*)pmyid);     int chunk = input.size()/thread_num;     int start = threadnum*chunk;     int end = start+chunk;     (i = start ; < end ; ++i)     {         output[i] = input[i]*-1;     }  } 

Comments