How do you use CreateThread for functions which are class members?

You need to create a static method to use as the actual thread start function, and pass a pointer to the instance as the lpParameter argument to CreateThread. That will get passed to the static method, which can cast it to an object pointer and call through to the member function.

class MyClass
{
    static DWORD WINAPI StaticThreadStart(void* Param)
    {
        MyClass* This = (MyClass*) Param;
        return This->ThreadStart();
    }

    DWORD ThreadStart(void)
    {
        // Do stuff
    }

    void startMyThread()
    {
       DWORD ThreadID;
       CreateThread(NULL, 0, StaticThreadStart, (void*) this, 0, &ThreadID);
    }
};

Leave a Comment