I can't pass a pointer to method to the CreateThread function, of course. What should I do?
If using a class, some pattern like this usually works well:
.h
static UINT __cdecl StaticThreadFunc(LPVOID pParam);
UINT ThreadFunc();
.cpp
// Somewhere you launch the thread
AfxBeginThread(
    StaticThreadFunc,
    this);  
UINT __cdecl CYourClass::StaticThreadFunc(LPVOID pParam)
{
    CYourClass *pYourClass = reinterpret_cast<CYourClass*>(pParam);
    UINT retCode = pYourClass->ThreadFunc();
    return retCode;
}
UINT CYourClass::ThreadFunc()
{ 
    // Do your thing, this thread now has access to all the classes member variables
}
                        I often do this:
class X {
private:
    static unsigned __stdcall ThreadEntry(void* pUserData) {
        return ((X*)pUserData)->ThreadMain();
    }
    unsigned ThreadMain() {
         ...
    }
};
And then I pass this as user data to the thread creator function (_beginthread[ex], CreateThread, etc)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With