title: C++ std::vector of independent std::threads
body: <p>I'm building a real time software where I have a main infinite loops on <code>main()</code> and threads used to read and process data.</p>
  <p>One of the issues is keeping a <code>std::vector</code> of running threads to send signals to them and to monitor execution. So I put together this code:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;thread&gt;
  #include &lt;chrono&gt;
  
  namespace readerThread {
  
      void start(int id)
      {
          while (1)
          {
              std::cout &lt;&lt; &quot;Reader &quot; &lt;&lt; id &lt;&lt; &quot; running...&quot; &lt;&lt;  std::endl;
              std::this_thread::sleep_for(std::chrono::milliseconds(1000));
          }
      }
  
  }
  
  
  int main() 
  {
  
          int readers[] = { 1, 2, 3 };
          
          std::vector&lt;std::thread&gt; readerThreads;
  
          for (int &amp;reader : readers)
          {
              std::thread th(readerThread::start, reader);
              readerThreads.push_back(th);
          }
  
          while(true)
          {
              std::cout &lt;&lt; &quot;Waiting...&quot; &lt;&lt; std::endl;
              std::this_thread::sleep_for(std::chrono::milliseconds(10000));
          }
          
          return 0;
  }
  </code></pre>
  <p>It doesn´t even compile, getting this error:</p>
  <pre><code>In file included from /usr/local/include/c++/5.1.0/x86_64-unknown-linux-gnu/bits/c++allocator.h:33:0,
                   from /usr/local/include/c++/5.1.0/bits/allocator.h:46,
                   from /usr/local/include/c++/5.1.0/string:41,
                   from /usr/local/include/c++/5.1.0/bits/locale_classes.h:40,
                   from /usr/local/include/c++/5.1.0/bits/ios_base.h:41,
                   from /usr/local/include/c++/5.1.0/ios:42,
                   from /usr/local/include/c++/5.1.0/ostream:38,
                   from /usr/local/include/c++/5.1.0/iostream:39,
                   from main.cpp:1:
  /usr/local/include/c++/5.1.0/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator&lt;_Tp&gt;::construct(_Up*, _Args&amp;&amp; ...) [with _Up = std::thread; _Args = {const std::thread&amp;}; _Tp = std::thread]':
  /usr/local/include/c++/5.1.0/bits/alloc_traits.h:256:4:   required from 'static std::_Require&lt;std::allocator_traits&lt;_Alloc&gt;::__has_construct&lt;_Tp, _Args ...&gt; &gt; std::allocator_traits&lt;_Alloc&gt;::_S_construct(_Alloc&amp;, _Tp*, _Args&amp;&amp; ...) [with _Tp = std::thread; _Args = {const std::thread&amp;}; _Alloc = std::allocator&lt;std::thread&gt;; std::_Require&lt;std::allocator_traits&lt;_Alloc&gt;::__has_construct&lt;_Tp, _Args ...&gt; &gt; = void]'
  /usr/local/include/c++/5.1.0/bits/alloc_traits.h:402:16:   required from 'static decltype (_S_construct(__a, __p, (forward&lt;_Args&gt;)(std::allocator_traits::construct::__args)...)) std::allocator_traits&lt;_Alloc&gt;::construct(_Alloc&amp;, _Tp*, _Args&amp;&amp; ...) [with _Tp = std::thread; _Args = {const std::thread&amp;}; _Alloc = std::allocator&lt;std::thread&gt;; decltype (_S_construct(__a, __p, (forward&lt;_Args&gt;)(std::allocator_traits::construct::__args)...)) = &lt;type error&gt;]'
  /usr/local/include/c++/5.1.0/bits/stl_vector.h:917:30:   required from 'void std::vector&lt;_Tp, _Alloc&gt;::push_back(const value_type&amp;) [with _Tp = std::thread; _Alloc = std::allocator&lt;std::thread&gt;; std::vector&lt;_Tp, _Alloc&gt;::value_type = std::thread]'
  main.cpp:37:30:   required from here
  /usr/local/include/c++/5.1.0/ext/new_allocator.h:120:4: error: use of deleted function 'std::thread::thread(const std::thread&amp;)'
    { ::new((void *)__p) _Up(std::forward&lt;_Args&gt;(__args)...); }
      ^
  In file included from main.cpp:4:0:
  /usr/local/include/c++/5.1.0/thread:126:5: note: declared here
       thread(const thread&amp;) = delete;
       ^
  </code></pre>
  <p>The threads are independent, so I don't need to call <code>join</code> on the main program nor on any thread...</p>
  <p>So, here are my doubts:</p>
  <p>Why my code does not compile?</p>
  <p>Is that the correct way to store the vector of threads ?</p>
  <p>Thanks for helping...</p>
  <p>PS:<a href="http://coliru.stacked-crooked.com/a/f8064cce3edd2c47" rel="nofollow noreferrer">Original code here</a>:</p>