lundi 31 janvier 2022

Vector of mutex to synchronize access to vector cells

I wrote code to do some parallel operations on a vector, my goal is to protect a single cell of a vector so other cells can be accessed in parallel, so I tried to use a vector of mutex of the same size of the other vector

vector<int> myIntVec(n,0);
vector<mutex> mtxVec(n);

then the critical section, each thread executes this (goal is to mark seen cells)

 for (i of something)
        {
           mtxVec[i].lock();
           if (myIntVec == 0 ){ 
                myIntVec[i]++;
                mtxVec[i].unlock();
               }
            else
              mtxVec[i].unlock();
         }

no other operations on these 2 vectors. but doing some tests, what i got is that myIntVec cells contain numbers greater than 1, when they should contain at least 1. What am I missing?

Aucun commentaire:

Enregistrer un commentaire