title: OpenMP function calls in parallel
body: <p>I'm looking for a way to call a function in parallel. </p>
  
  <p>For example, if I have 4 threads, I want to each of them to call the same function with their own thread id as an argument. </p>
  
  <p>Because of the argument, no thread will work on the same data. </p>
  
  <pre><code>#pragma omp parallel
  {
      for(int p = 0; p &lt; numberOfThreads; p++)
      {
          if(p == omp_get_thread_num())
              parDF(p);
      }
  }
  </code></pre>
  
  <p>Thread 0 should run parDF(0)</p>
  
  <p>Thread 1 should run parDF(1)</p>
  
  <p>Thread 2 should run parDF(2)</p>
  
  <p>Thread 3 should run parDF(3)</p>
  
  <p>All this should be done at the same time... </p>
  
  <p>This (obviously) doesn't work, but what is the right way to do parallel function calls? </p>
  
  <p>EDIT: The actual code (This might be too much information... But it was asked for...) </p>
  
  <p>From the function that calls parDF():</p>
  
  <pre><code>omp_set_num_threads(NUM_THREADS);
  #pragma omp parallel
  {
  
      numberOfThreads = omp_get_num_threads();
      //split nodeQueue
      #pragma omp master
      {
          splitNodeQueue(numberOfThreads);
      }
      int tid = omp_get_thread_num();
  
      //printf("Hello World from thread = %d\n", tid);
      #pragma omp parallel for private(tid)
      for(int i = 0; i &lt; numberOfThreads; ++i)
      {
              parDF(tid, originalQueueSize, DFlevel);
      }
  }
  </code></pre>
  
  <p>The parDF function:</p>
  
  <pre><code>bool Tree::parDF(int id, int originalQueueSize, int DFlevel)
  {
  double possibilities[20];
  double sequence[3];
  double workingSequence[3];
  int nodesToExpand = originalQueueSize/omp_get_num_threads();
  int tenthsTicks = nodesToExpand/10;
  int numPossibilities = 0;
  int percentage = 0;
  list&lt;double&gt;::iterator i;
  list&lt;TreeNode*&gt;::iterator n;
  
  cout &lt;&lt; "My ID is: "&lt;&lt; omp_get_thread_num() &lt;&lt; endl;
  
          while(parNodeQueue[id].size() &gt; 0 and parNodeQueue[id].back()-&gt;depth == DFlevel)
          {
  
              if(parNodeQueue[id].size()%tenthsTicks == 0)
              {
                  cout &lt;&lt; endl;
                  cout &lt;&lt; percentage*10 &lt;&lt; "% done..." &lt;&lt; endl;
                  if(percentage == 10)
                  {
                      percentage = 0;
                  }
                  percentage++;
              }
  
              //countStartPoints++;
              depthFirstQueue.push_back(parNodeQueue[id].back());
              numPossibilities = 0;
  
              for(i = parNodeQueue[id].back()-&gt;content.sortedPoints.begin(); i != parNodeQueue[id].back()-&gt;content.sortedPoints.end(); i++)
              {
  
                  for(int j = 0; j &lt; deltas; j++)
                  {
                      if(parNodeQueue[id].back()-&gt;content.doesPointExist((*i) + delta[j]))
                      {
                          for(int k = 0; k &lt;= numPossibilities; k++)
                          {
                              if(fabs((*i) + delta[j] - possibilities[k]) &lt; 0.01)
                              {
                                  goto pointAlreadyAdded;
                              }
                          }
                          possibilities[numPossibilities] = ((*i) + delta[j]);
                          numPossibilities++;
                          pointAlreadyAdded:
                          continue;
                      }
                  }
              }
  
              // Out of the list of possible points. All combinations of 3 are added, building small subtrees in from the node.
              // If a subtree succesfully breaks the lower bound, true is returned.
  
              for(int i = 0; i &lt; numPossibilities; i++)
              {
                  for(int j = 0; j &lt; numPossibilities; j++)
                  {
                      for(int k = 0; k &lt; numPossibilities; k++)
                      {
                          if( k != j and j != i and i != k)
                          {
                              sequence[0] = possibilities[i];
                              sequence[1] = possibilities[j];
                              sequence[2] = possibilities[k];
                              //countSeq++;
                              if(addSequence(sequence, id))
                              {
                                  //successes++;
                                  workingSequence[0] = sequence[0];
                                  workingSequence[1] = sequence[1];
                                  workingSequence[2] = sequence[2];
                                  parNodeQueue[id].back()-&gt;workingSequence[0] = sequence[0];
                                  parNodeQueue[id].back()-&gt;workingSequence[1] = sequence[1];
                                  parNodeQueue[id].back()-&gt;workingSequence[2] = sequence[2];
                                  parNodeQueue[id].back()-&gt;live = false;
                                  succesfulNodes.push_back(parNodeQueue[id].back());
                                  goto nextNode;
                              }
                              else
                              {
                                  destroySubtree(parNodeQueue[id].back());
                              }
                          }
                      }
                  }
              }
              nextNode:
              parNodeQueue[id].pop_back();
          }
  </code></pre>