代码
#include <iostream> // std::cout
#include <thread> // std::thread
using namespace std;
void foo(int &a)
{
cout<<"arguement:"<<a<<endl;
}
int main()
{
int a=3;
std::thread first (foo,a);
first.join();
return 0;
}
编译时出错
g++ a.cpp -std=c++11
In file included from d:\build chan tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\thread:39:0,
from a.cpp:2:
d:\build chan tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\functional: In instantiation of 'struct std::_Bind_simple<void (*())(int&)>':
d:\build chan tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\thread:137:47: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(int&); _Args = {}]'
a.cpp:11:25: required from here
d:\build chan tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\functional:1697:61: error: no type named 'type' in 'class std::result_of<void (*())(int&)>'
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
d:\build chan tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\functional:1727:9: error: no type named 'type' in 'class std::result_of<void (*())(int&)>'
_M_invoke(_Index_tuple<_Indices...>)
对于c++的thread类使用引用作为线程函数参数,需要用std::ref()包括参数