Alejandro
Alejandro

Reputation: 31

How to create a thread passing a member function of a specified object?

I have been looking for some information about this issue and researching on the internet, but I didn't find anything that could solve my question.

In summary, I have two classes, the first class is used as an Interface between cameras and the computer. We could call it, class CCameraInterface. Second class is used to handle cameras. We could call it class CMultiCam.

In the class CCameraInterface I create many CMultiCam objects as cameras from different transport layers (i.e. USB3.0 GigE ...), and these objects are stored in a std::vector called m_vCamera. Although they are the same kind of object, they will do different stuff in the same functions. Hence, I need to identify which object I am calling in a new thread.

Class CMultiCamera{
public:
...
SI_32 GetFrames(std::vector<UI_8*>& f_desiredFrame_p);
...
}

I tried to use it inside CCameraInterface:

std::thread* l_thCaptureImages = new std::thread[m_vCameraConfig.size()];
for (int l_index = 0; l_index < m_vCameraConfig.size(); l_index++) {

l_thCaptureImages[l_index] = std::thread(&CGenIMultiCamera::GetFrame, this, l_index, m_vImage);
m_mutexLock.lock();
s_writableAddress++;
m_mutexLock.unlock();
}

By this way, I am not calling the function GetFrame of a specific object, but just a generic function from a class CGenIMultiCamera.

So I tried to run this line instead:

l_thCaptureImages[l_index] = std::thread(&CGenIMultiCamera::GetFrames, (this->(m_vCamera.at(l_index)), m_vImage);

But I am getting the next compiler error:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(249): note: see the reference to create function template instances void std::_LaunchPad<_Target>::_Execute<0,1>(std::tuple<SI_32 (__cdecl CGenIMultiCamera::* )(std::vector<UI_8 *,std::allocator<_Ty>> &),CABSCameraControl *> &,std::integer_sequence<unsigned __int64,0,1>)'
1>          with
1>          [
1>              _Target=std::unique_ptr<std::tuple<SI_32 (__cdecl 
CGenIMultiCamera::* )(std::vector<UI_8 *,std::allocator<unsigned char *>> &),CABSCameraControl *>,std::default_delete<std::tuple<SI_32 (__cdecl CGenIMultiCamera::* )(std::vector<UI_8 *,std::allocator<unsigned char *>> &),CABSCameraControl *>>>,
1>              _Ty=UI_8 *
1>          ]

How can I solve it? Thank so much in advance

Upvotes: 0

Views: 174

Answers (2)

catnip
catnip

Reputation: 25388

Try passing the address of a static member function plus the address of your object. Then, from this function, you can call the member function you wanted to call in the first place.

Example code:

#include <thread>
#include <iostream>

class MyClass
{
public:
    static void StaticFunc (MyClass *self) { self->MemberFunc (); };
    void MemberFunc () { std::cout << "MemberFunc called\n"; }
};

int main ()
{
    MyClass obj;
    std::thread t (&MyClass::StaticFunc, &obj);
    t.join ();
}

Live demo

Pass additional parameters to MemberFunc via StaticFunc as needed.

Upvotes: 1

rustyx
rustyx

Reputation: 85341

Use a lambda:

l_thCaptureImages[l_index] = std::thread([this, l_index] {
    GetFrames(m_vCamera.at(l_index)), m_vImage);
});

Upvotes: 1

Related Questions