PinkTurtle
PinkTurtle

Reputation: 7041

New to threads : problems using std::ref() in thread parameters

I am new to C++ threads and trying make several objects packed in a std::vector call their prepareForRendering(...) method asynchronously. It seems I am using std::ref wrongly. This works :

for (ObjMesh& mesh : this->meshes) {

    // Prepare mesh
    std::thread t(&ObjMesh::prepareForRendering, mesh, pDevice, prepareTextures, prepareVertices, prepareBuffers, filter, addressMode);
    t.join();
}

This doesn't :

for (ObjMesh& mesh : this->meshes) {

    // Prepare mesh
    std::thread t(&ObjMesh::prepareForRendering, std::ref(mesh), pDevice, prepareTextures, prepareVertices, prepareBuffers, filter, addressMode);
    t.join();
}

Compiler throws :

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>          ]

I need to make sure the ObjMesh instance is passed by reference inside the thread.

(I know my thread model makes no sense - this is an example). TYVM.

EDIT The prepare for rendering function.

bool ObjMesh::prepareForRendering(ID3D11Device* pDevice, bool prepareTextures,  bool prepareVertices, bool prepareBuffers, D3D11_FILTER filter, D3D11_TEXTURE_ADDRESS_MODE addressMode);

Upvotes: 0

Views: 313

Answers (1)

Slava
Slava

Reputation: 44238

Just pass pointer to bind:

std::thread t(&ObjMesh::prepareForRendering, &mesh, pDevice, prepareTextures, prepareVertices, prepareBuffers, filter, addressMode);

Upvotes: 2

Related Questions