线程新手:在线程参数中使用 std::ref() 的问题
New to threads : problems using std::ref() in thread parameters
我是 C++ 线程的新手,正在尝试使打包在 std::vector
中的多个对象异步调用它们的 prepareForRendering(...)
方法。看来我用错了std::ref
。这有效:
for (ObjMesh& mesh : this->meshes) {
// Prepare mesh
std::thread t(&ObjMesh::prepareForRendering, mesh, pDevice, prepareTextures, prepareVertices, prepareBuffers, filter, addressMode);
t.join();
}
这不是 :
for (ObjMesh& mesh : this->meshes) {
// Prepare mesh
std::thread t(&ObjMesh::prepareForRendering, std::ref(mesh), pDevice, prepareTextures, prepareVertices, prepareBuffers, filter, addressMode);
t.join();
}
编译器抛出:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: With the following template arguments:
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: '_Callable=bool (__thiscall ObjMesh::* )(ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE)'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: '_Types={std::reference_wrapper<ObjMesh>, ID3D11Device *, bool, bool, bool, D3D11_FILTER, D3D11_TEXTURE_ADDRESS_MODE}'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(247): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0,1,2,3,4,5,6,7>(std::tuple<bool (__thiscall ObjMesh::* )(ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE),std::reference_wrapper<ObjMesh>,ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE> &,std::integer_sequence<_Ty,0,1,2,3,4,5,6,7>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<bool (__thiscall ObjMesh::* )(ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE),std::reference_wrapper<ObjMesh>,ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE>,std::default_delete<std::tuple<bool (__thiscall ObjMesh::* )(ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE),std::reference_wrapper<ObjMesh>,ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE>>>,
1> _Ty=size_t
1> ]
我需要确保 ObjMesh
实例在线程内通过引用传递。
(我知道我的线程模型没有意义 - 这是一个例子)。 TYVM.
EDIT 准备渲染函数。
bool ObjMesh::prepareForRendering(ID3D11Device* pDevice, bool prepareTextures, bool prepareVertices, bool prepareBuffers, D3D11_FILTER filter, D3D11_TEXTURE_ADDRESS_MODE addressMode);
只需将指针传递给绑定:
std::thread t(&ObjMesh::prepareForRendering, &mesh, pDevice, prepareTextures, prepareVertices, prepareBuffers, filter, addressMode);
我是 C++ 线程的新手,正在尝试使打包在 std::vector
中的多个对象异步调用它们的 prepareForRendering(...)
方法。看来我用错了std::ref
。这有效:
for (ObjMesh& mesh : this->meshes) {
// Prepare mesh
std::thread t(&ObjMesh::prepareForRendering, mesh, pDevice, prepareTextures, prepareVertices, prepareBuffers, filter, addressMode);
t.join();
}
这不是 :
for (ObjMesh& mesh : this->meshes) {
// Prepare mesh
std::thread t(&ObjMesh::prepareForRendering, std::ref(mesh), pDevice, prepareTextures, prepareVertices, prepareBuffers, filter, addressMode);
t.join();
}
编译器抛出:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: With the following template arguments:
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: '_Callable=bool (__thiscall ObjMesh::* )(ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE)'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: '_Types={std::reference_wrapper<ObjMesh>, ID3D11Device *, bool, bool, bool, D3D11_FILTER, D3D11_TEXTURE_ADDRESS_MODE}'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(247): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0,1,2,3,4,5,6,7>(std::tuple<bool (__thiscall ObjMesh::* )(ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE),std::reference_wrapper<ObjMesh>,ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE> &,std::integer_sequence<_Ty,0,1,2,3,4,5,6,7>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<bool (__thiscall ObjMesh::* )(ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE),std::reference_wrapper<ObjMesh>,ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE>,std::default_delete<std::tuple<bool (__thiscall ObjMesh::* )(ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE),std::reference_wrapper<ObjMesh>,ID3D11Device *,bool,bool,bool,D3D11_FILTER,D3D11_TEXTURE_ADDRESS_MODE>>>,
1> _Ty=size_t
1> ]
我需要确保 ObjMesh
实例在线程内通过引用传递。
(我知道我的线程模型没有意义 - 这是一个例子)。 TYVM.
EDIT 准备渲染函数。
bool ObjMesh::prepareForRendering(ID3D11Device* pDevice, bool prepareTextures, bool prepareVertices, bool prepareBuffers, D3D11_FILTER filter, D3D11_TEXTURE_ADDRESS_MODE addressMode);
只需将指针传递给绑定:
std::thread t(&ObjMesh::prepareForRendering, &mesh, pDevice, prepareTextures, prepareVertices, prepareBuffers, filter, addressMode);