title: Wrap std::thread call function
body: <p>I would like to control <code>threads</code>, so each new issued <code>thread</code> will go through my code first.<br>
  This way, each new thread issued with <code>runThread()</code> in the example below, will first call the function <code>runInnerThread()</code> below (in which I do some kind of initializations) and then call the desired function.</p>
  
  <p>I've tried putting something like this:  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  
  template&lt;typename _Callable, typename... _Args&gt;
  void runThreadInner(_Callable&amp;&amp; __f, _Args&amp;&amp;... __args) {
      // My initializations...
      __f(__args...);
      // My finishing...
  };
  
  template&lt;typename _Callable, typename... _Args&gt;
  bool runThread(_Callable&amp;&amp; __f, _Args&amp;&amp;... __args) {
      std::thread t(std::bind(&amp;runThreadInner,
                              std::forward&lt;_Callable&gt;(__f),
                              std::forward&lt;_Args&gt;(__args)...));
  }
  
  int main() {
      runThread([]() {
          std::cout &lt;&lt; std::this_thread::get_id() &lt;&lt; "Threading...\n";
      });
      return 0;
  }
  </code></pre>
  
  <p>I get an error from the compiler which complains about deducing 
   <code>runThreadInner()</code> templates</p>
  
  <pre><code>main.cpp: In instantiation of ‘bool runThread(_Callable&amp;&amp;, _Args&amp;&amp; ...) [with _Callable = main()::__lambda0; _Args = {}]’:
  main.cpp:43:3:   required from here
  main.cpp:27:38: error: no matching function for call to ‘bind(&lt;unresolved overloaded function type&gt;, main()::__lambda0)’
          std::forward&lt;_Args&gt;(__args)...));
                                        ^
  main.cpp:27:38: note: candidates are:
  In file included from /usr/include/c++/4.8/memory:79:0,
                   from main.cpp:2:
  /usr/include/c++/4.8/functional:1655:5: note: template&lt;class _Func, class ... _BoundArgs&gt; typename std::_Bind_helper&lt;std::__or_&lt;std::is_integral&lt;typename std::decay&lt;_Tp&gt;::type&gt;, std::is_enum&lt;typename std::decay&lt;_Tp&gt;::type&gt; &gt;::value, _Func, _BoundArgs ...&gt;::type std::bind(_Func&amp;&amp;, _BoundArgs&amp;&amp; ...)
       bind(_Func&amp;&amp; __f, _BoundArgs&amp;&amp;... __args)
       ^
  /usr/include/c++/4.8/functional:1655:5: note:   template argument deduction/substitution failed:
  main.cpp:27:38: note:   couldn't deduce template parameter ‘_Func’
          std::forward&lt;_Args&gt;(__args)...));
                                        ^
  In file included from /usr/include/c++/4.8/memory:79:0,
                   from main.cpp:2:
  /usr/include/c++/4.8/functional:1682:5: note: template&lt;class _Result, class _Func, class ... _BoundArgs&gt; typename std::_Bindres_helper&lt;_Result, _Func, _BoundArgs&gt;::type std::bind(_Func&amp;&amp;, _BoundArgs&amp;&amp; ...)
       bind(_Func&amp;&amp; __f, _BoundArgs&amp;&amp;... __args)
       ^
  /usr/include/c++/4.8/functional:1682:5: note:   template argument deduction/substitution failed:
  main.cpp:27:38: note:   couldn't deduce template parameter ‘_Result’
          std::forward&lt;_Args&gt;(__args)...));
  </code></pre>
  
  <p>I tried explicitly defining the template, without any success:</p>
  
  <pre><code>template&lt;typename _Callable, typename... _Args&gt;
  bool runThread(_Callable&amp;&amp; __f, _Args&amp;&amp;... __args) {
      std::thread t(std::bind(&amp;runThreadInner&lt;_Callable, _Args&gt;,
                              std::forward&lt;_Callable&gt;(__f),
                              std::forward&lt;_Args&gt;(__args)...));
  }
  </code></pre>
  
  <p>Is it possible?
  Thanks.</p>