Threads and Classes

I'm having a bit of trouble with this one. When a member function of my class is called, the function's only job is to create a thread. The the function I specify in CreateThread is a member function of the same class, but it wont work. I get this error:

Info :Making...
Info :Compiling C:BC5BINBGPgp.cpp
Error: bgp.cpp(903,58):Member function must be called or its address taken


Now the only code that produces this error is as follows:

bool BGP::RunThread()
{
CreateThread( NULL,
0,
(LPTHREAD_START_ROUTINE)MyThread,
(LPVOID) hWnd,
0,
&Tid1);
return true;
}

DWORD BGP::MyThread()
{
return 0;
}

What have I done wrong?


Comments

  • : I'm having a bit of trouble with this one. When a member function of my class is called, the function's only job is to create a thread. The the function I specify in CreateThread is a member function of the same class, but it wont work. I get this error:
    :
    : Info :Making...
    : Info :Compiling C:BC5BINBGPgp.cpp
    : Error: bgp.cpp(903,58):Member function must be called or its address taken
    :
    :
    : Now the only code that produces this error is as follows:
    :
    : bool BGP::RunThread()
    : {
    : CreateThread( NULL,
    : 0,
    : (LPTHREAD_START_ROUTINE)MyThread,
    : (LPVOID) hWnd,
    : 0,
    : &Tid1);
    : return true;
    : }
    :
    : DWORD BGP::MyThread()
    : {
    : return 0;
    : }
    :
    : What have I done wrong?
    :
    :

    The thread function has a predefined format and cannot be a function of any class. To circumvent this you have to pass 'this' pointer as a thread parameter:
    [code]
    DWORD MyThreadProc (LPVOID pThis)
    {
    BGP* pClass = (BGP*) pThis;
    // Now you can use 'pClass' to access its members
    }

    bool BGP::RunThread()
    {
    CreateThread( NULL,
    0,
    (LPTHREAD_START_ROUTINE)MyThreadProc,
    (LPVOID) this, // That is where connection is!
    0,
    &Tid1);
    return true;
    }
    [/code]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion