Threading a member function with a std::mutex as an argument
<p>So I have an object with a member function I want to thread. Since this function will be manipulating some resource outside of the object, I want to pass a mutex by reference as an argument to this function:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;mutex&gt;
#include &lt;thread&gt;

class foo
{
    public:
        void bar( std::mutex &amp;m )
        {
            std::lock_guard&lt;std::mutex&gt; lock( m );
            std::cout &lt;&lt; "Threading this function works!" &lt;&lt; std::endl;
        }
};


int main()
{
    foo a;
    std::mutex m;
    std::thread bar_thread( &amp;foo::bar, std::ref( a ), std::ref( m ) );
    bar_thread.join();
    return 0;
}
</code></pre>

<p>This compiles and runs fine in Visual Studio 2013/VC++. However when I try and compile this in g++ it fails. The error message is also very cryptic which makes it very difficult to understand what the compiler is complaining about:</p>

<pre><code>/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple&lt;std::_Mem_fn&lt;void (foo::*)(std::mutex&amp;)&gt;(std::reference_wrapper&lt;foo&gt;, std::reference_wrapper&lt;std::mutex&gt;)&gt;’:
/usr/include/c++/4.8/thread:137:47:   required from ‘std::thread::thread(_Callable&amp;&amp;, _Args&amp;&amp; ...) [with _Callable = void (foo::*)(std::mutex&amp;); _Args = {std::reference_wrapper&lt;foo&gt;, std::reference_wrapper&lt;std::mutex&gt;}]’
thread_test.cpp:63:69:   required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of&lt;std::_Mem_fn&lt;void (foo::*)(std::mutex&amp;)&gt;(std::reference_wrapper&lt;foo&gt;, std::reference_wrapper&lt;std::mutex&gt;)&gt;’
      typedef typename result_of&lt;_Callable(_Args...)&gt;::type result_type;
                                                            ^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of&lt;std::_Mem_fn&lt;void (foo::*)(std::mutex&amp;)&gt;(std::reference_wrapper&lt;foo&gt;, std::reference_wrapper&lt;std::mutex&gt;)&gt;’
        _M_invoke(_Index_tuple&lt;_Indices...&gt;)
        ^
</code></pre>

<p>I have a suspicion it has something to do with the non-copyability of std::mutex and perhaps the std::ref implementation in g++ is different to the one in vc++? This is just a random guess though.</p>

<p>Does anyone who is well versed in the subtleties of the two different C++ compilers know what is causing this problem, and how it can be resolved?</p>