title: Can i calculate the average of multi-thread numbers?
body: <p>I am using multi-threads function in C++. Using <code>n</code> multi-threads, i have <code>n</code> random output. I need to calculate the mean of the multi-threads output in the code. let suppose for <code>n=4</code> threads, the code is</p>
  
  <pre><code>#include &lt;omp.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;random&gt;
  #include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;iomanip&gt;
  #include &lt;array&gt;
  #include &lt;eigen3/Eigen/Dense&gt;
  
  #define         W               1.0
  #define         avg_disorder    10
  #define         numThd          4
  
  int main()
  {
    #pragma omp parallel num_threads(numThd)
    {
      // define random numbers
      std::mt19937 rng;                  
      std::uniform_real_distribution &lt;&gt; dist;
      std::random_device r;
      std::array&lt;int,624&gt; seed_data;
      std::generate(seed_data.begin(), seed_data.end(), std::ref(r));
      std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
      rng.seed(seq);
  
      Eigen::Array&lt;double, -1, 1&gt; rp; // rp= random potential
      rp  = Eigen::Array&lt;double, -1, 1&gt;::Zero(avg_disorder, 1);
  
      //List of 10 random numbers
      for(int avr = 0; avr &lt; avg_disorder; avr++ )
      {
        rp(avr,0) = 0.5* W* (-1 + 2* dist(rng) );
      }
      // output: the mean of the rp
      #pragma omp critical
      { 
        FILE *output;
        char name[50];
        sprintf(name, "avgW%06.2f.dat", W);    
        output = fopen(name, "a");
        fprintf(output, "%e \n", rp.mean() );
        fclose(output);     
      }
  
    }
  
    return 1;
  }
  
  // run the file avgThreads
  //g++ -std=c++0x -fopenmp -o avgThreads avgThreads.cpp
  </code></pre>
  
  <blockquote>
    <p>Output: 4 threads: 4 results (4 different mean of the rp).</p>
  </blockquote>
  
  <pre><code>1.015983e-01
  4.097469e-02
  -1.275186e-01
  -1.243190e-01
  </code></pre>
  
  <blockquote>
    <p>My Question is:</p>
  </blockquote>
  
  <p>Can i calculate the average of the multi-threads output in the code?</p>