0

I want to push_back an object into a vector from different threads. The no. of threads depends on the machine.

#pragma omp parallel shared(Spaces, LookUpTable) private(LutDistribution, tid)
{
    tid = omp_get_thread_num();

    BestCoreSpaces.push_back( computeBestCoreSpace(tid, &Spaces, &LookUpTable, LutDistribution));
}

The problem is, that I'm not sure if it's working. I don't get crashes. I'm using openMP. Is openMP queuing something? Maybe its enough to reserve memory for the container with BestCoreSpaces.reserve(tid) or to assign the amount of elements with BestCoreSpaces.assign(tid, Space). Can somebody help me?

1 Answer 1

4

You just get away with it - you have a race condition that might or might not manifest itself depending on the optimization level at compile time, the thread execution and/or the alignment of the stars.

You have to make the push_back() a critical section (i.e use a mutex). For example:

#pragma omp parallel shared(Spaces, LookUpTable, BestCoreSpaces) private(LutDistribution, tid)
{
  tid = omp_get_thread_num();
#pragma omp critical
  BestCoreSpaces.push_back( 
    computeBestCoreSpace(tid, &Spaces, &LookUpTable, LutDistribution)
  );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.