1

I am new in threading and classes, and trying to create a class. I have two member function in which one call the other using thread.

    bool CSocket::StartCamera()
{
    bool m_SendFlag;
    m_SocketIn = socketInitialize(m_CameraIP, m_CameraPort);
    if (m_SocketIn == INVALID_SOCKET)
        return false;
    m_SendFlag = sendCommand(m_SocketIn);
    if (!m_SendFlag)
        return false;
    std::thread acquiringThread(&CSocket::callReceiveFxn, m_SocketIn);
    acquiringThread.join();
}

and other member function is

 void CSocket::callReceiveFxn(SOCKET socket)
{
    unsigned char *m_frameBuffer = NULL;
    while (true)
    {
        m_frameBuffer = receivePacket(socket);
        m_ImageQ.Enqueue(m_frameBuffer, MAX_BYTE_PER_FRAME);
    }
}

So my StartCamera function call the callReceiveFxn using thread. But at std::thread acquiringThread(&CSocket::callReceiveFxn, m_SocketIn); error occur that says Error 3 error C2064: term does not evaluate to a function taking 1 arguments

So can anyone tell me why this error comes and please tell the solution for that

Lucky
  • 49
  • 7
  • 2
    You create a thread only to immediately wait for it to finish. The thread also happens to never return. This doesn't make sense. – Passer By Jul 16 '18 at 10:09
  • 1
    The error is telling you `CSocket::callReceiveFxn` is not a function taking one argument. It is a _member_ function, and they're different. – Passer By Jul 16 '18 at 10:10
  • @PasserBy because it continuously take image frame I will use IPC later to stop the thread – Lucky Jul 16 '18 at 10:15
  • so how to call that member function using thread – Lucky Jul 16 '18 at 10:16
  • Possible dupe of ["Start thread with member function "](https://stackoverflow.com/questions/10673585/start-thread-with-member-function). – G.M. Jul 16 '18 at 10:24
  • 1
    Aside: Why do you use the `m_` prefix for *some* local variables? That's *actively misleading* use of Hungarian notation – Caleth Jul 16 '18 at 10:35
  • Possible duplicate of [Start thread with member function](https://stackoverflow.com/questions/10673585/start-thread-with-member-function) – Passer By Jul 16 '18 at 11:10

3 Answers3

0

You can't do that with member functions, as the error is stating. But since you're using std::thread, I'm certain you're using at least C++11. Therefore, you have access to lambdas.

You can simply call the member in a lambda and pass that lambda to the constructor of std::thread.

Demosthenes
  • 1,515
  • 10
  • 22
0

You need to pass class instance pointer:

std::thread acquiringThread(&CSocket::callReceiveFxn, this, m_SocketIn);
user7860670
  • 35,849
  • 4
  • 58
  • 84
0

You are missing a value for the (implicit) this parameter of callReceiveFxn.

You also wait for the thread to finish before returning, the thread is superfluous. Perhaps you want the thread to continue after StartCamera finishes?

You fail to return a value at the end of the function.

I don't like the hungarian notation for members, but even worse is using it for locals.

bool CSocket::StartCamera()
{
    m_SocketIn = socketInitialize(m_CameraIP, m_CameraPort);
    if (m_SocketIn == INVALID_SOCKET)
        return false;
    if (!sendCommand(m_SocketIn))
        return false;
    std::thread(&CSocket::callReceiveFxn, this, m_SocketIn).detach();
    return true;
}

Note that callReceiveFxn might not need a parameter, it can access m_SocketIn

Caleth
  • 52,200
  • 2
  • 44
  • 75
  • Sir the StartCamera() is calling from main function if I use detach then both main and callReceiveFxn work concurrently – Lucky Jul 16 '18 at 12:10