title: Pre-allocated private std::vector in OpenMP parallelized for loop in C++
body: <p>I intend to use buffers <code>std::vector&lt;size_t&gt; buffer(100)</code>, one in each thread in a parallelization of a loop, as suggested by this code:</p>
  
  <pre><code>std::vector&lt;size_t&gt; buffer(100);
  #pragma omp parallel for private(buffer)
  for(size_t j = 0; j &lt; 10000; ++j) {
      // ... code using the buffer ...
  }
  </code></pre>
  
  <p>This code does not work. Although there is a buffer for every thread, those can have size 0. </p>
  
  <p>How can I allocate the buffer in the beginning of each thread? Can I still use <code>#pragma omp parallel for</code>? And can I do it more elegantly than this:</p>
  
  <pre><code>std::vector&lt;size_t&gt; buffer;
  #pragma omp parallel for private(buffer)
  for(size_t j = 0; j &lt; 10000; ++j) {
      if(buffer.size() != 100) {
          #pragma omp critical
          buffer.resize(100);
      }
      // ... code using the buffer ...
  }
  </code></pre>