0

The following toy code does nothing useful that I am aware of:

#include <iostream>

#include "omp.h"

using namespace std;

class OtherClass {

   public:

   OtherClass() {
      cout << "hi\n";
   }

   int GetNumber(int input) {
      int i, num;
      int *tmp;

      tmp = new int[100];

      tmp[0] = 0;
      for (i=1;i<100;i++) {
         tmp[i] = (input*tmp[i-1]+1)%5;
      }
      num = tmp[input];

      delete[] tmp;

      return num;
   }

   ~OtherClass() {
      cout << "bye\n";
   }

};


class MainClass {

   public:

   int *myarray;

   MainClass(int numcells) {
      myarray = new int[numcells];
   }

   ~MainClass() {
      delete[] myarray;
   }

};


int main() {

   int i;

#define NUMELEMS 100

   MainClass *mc = new MainClass(NUMELEMS);
   OtherClass *oc = new OtherClass();

#pragma omp parallel private(i)
   { 
#pragma omp for
   for (i=0; i<NUMELEMS; i++ ) {
      mc->myarray[i] = oc->GetNumber(omp_get_thread_num()+1);
   }
   } // end of omp parallel

   for (i=0; i<NUMELEMS; i++) {
      cout << mc->myarray[i] << "\n";
   }

   delete mc;
   delete oc;

}

But it illustrates a question that has arisen in my mind while dealing with a real code. Namely, I am wondering about the array tmp in OtherClass:GetNumber. The details of how tmp is populated are unimportant; I was just putting in some code to produce some "interesting" numbers that would somehow vary from thread to thread. My question is, is tmp adequately protected from the various threads that might be running? Is it possible that the threads will be tripping over each other as they initialize, access, and delete it? Will one thread access tmp as defined by another thread, for instance? Or might one thread delete tmp before another thread can access it? Can someone tell me how I should modify the code, if modifications are needed?

This code runs fine by the way. But I am wondering what might happen in a much more complicated, larger code.

1 Answer 1

1

Your code is perfectly safe.

Anything that happens in GetNumber is completely local to one thread, no variable or pointer escapes. To be precise, the variable tmp is private to each thread - other threads do not see it. Consequently, the data pointed to by tmp, which is allocated by each thread, is not visible to any other thread.

That said - if the code is any indication of real code, I would highly recommend using less new / delete and placing more objects on the stack. It makes reasoning about the code much easier - especially involving threads.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. Don't think I can do much about the new/delete. It's not my code. Thanks.
@bob if you really have to do memory allocation, make sure that you are using a thread-aware memory allocator. infoworld.com/article/3201285/software/… seems a useful place to start.
@JimCownie Thanks very much for the tip. Will look into it.

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.