title: Large number of threads in C++ and Efficiency
body: <p>I've currently written a program in C++ that sometimes uses over 300 threads. In my program, I have an array of structs and the length of the array equals the number of threads. Let's assume that I have 400 structs and therefore, 400 threads. </p>
  
  <p>In a single iteration of a for loop, I apply a function to each of the 400 structs, and this function is executed in a thread. Therefore, I have 400 threads running concurrently.
  (I am using the boost thread library).</p>
  
  <p>I've tried to give a breakdown of what my code looks like (it is not the actual code):</p>
  
  <pre><code>struct my_struct{
    // Structure's members
  };
  
  std::vector&lt;my_struct&gt; my_vec;
  
  void my_fun(my_struct* my_str){
  // Operations on my_str
  }
  
  int main(){
    std::vector&lt;boost::thread&gt; thr(400);
    for (int k = 0; k &lt; 300; k++){
      for (int i = 0; i &lt; 400; i++){
        thr.at(i) = boost::thread(my_fun, &amp;my_vec.at(i));
        }
      }
  
      for (int m = 0; m &lt; M; m++){
        thr.at(m).join();
      }
    }
  }
  </code></pre>
  
  <p>The function I am using is computationally intensive, and from the code above, I use 400 threads to do calculations and this is done 300 times. Is there any more efficient way of performing this task? I'm not sure if having so many threads active at a single time may affect performance. I've heard of the threadpool library, but I'm not sure whether it'll provide any benefit to me. Any help is appreciated.</p>
  
  <p>Thank You Very Much.</p>