0

I have the following function that I would like to run by multiple threads:

float * readWrite(int & idx, const float * vector, const int & dimension){
    float * subPart = new float[dimension];
    for(int i=0; i < dimension; i++)
        subPart[i] = *(v+idx*dimension+i);
    idx++;
    return subPart;
}

So Each thread will read the variable idx and also increment it. I would like that at no time should readers and writers be allowed in at the same time. The solution would be that all threads would be able to access to idx and actually create a critical section (boost::mutex) only when idx changes. How could I do that with boost or even for C++11 std::mutex avoiding both the dead lock and slower down the reading access.

Thanks

saloua
  • 2,433
  • 4
  • 27
  • 37

0 Answers0