title: Extend the life of threads with synchronization (C++11)
body: <p>I have a program with a function which takes a pointer as arg, and a main. The main is creating <strong>n</strong> threads, each of them running the function on different memory areas depending on the passed <code>arg</code>. Threads are then joined, the main performs some data mixing between the area and creates <strong>n</strong> new threads which do the the same operation as the old ones.</p>
  
  <p>To improve the program I would like to keep the threads alive, removing the long time necessary to create them. Threads should sleep when the main is working and notified when they have to come up again. At the same way the main should wait when threads are working as it did with join.</p>
  
  <p>I cannot end up with a strong implementation of this, always falling in a deadlock.</p>
  
  <p>Simple baseline code, any hints about how to modify this would be much appreciated</p>
  
  <pre><code>#include &lt;thread&gt;
  #include &lt;climits&gt;
  
  ...
  
  void myfunc(void * p) {
    do_something(p);
  }
  
  int main(){
    void * myp[n_threads] {a_location, another_location,...};
    std::thread mythread[n_threads];
    for (unsigned long int j=0; j &lt; ULONG_MAX; j++) {
      for (unsigned int i=0; i &lt; n_threads; i++) {
        mythread[i] = std::thread(myfunc, myp[i]);
      }
      for (unsigned int i=0; i &lt; n_threads; i++) {
        mythread[i].join();
      }
      mix_data(myp); 
    }
    return 0;
  }
  </code></pre>