Creating a thread of a non-static member function?

If I have a member function.,.

MyClass::MyFunction()
{
    while(1)
    {
        //blah blah blah
    }
}

      

. and I am trying to create a thread of this function.,.

CreateThread(Null, 0, (LPTHREAD_START_ROUTINE)MyFunction, NULL, 0, NULL);

      

... I always get the error (LPTREAD_START_ROUTINE) MyFunction is an invalid cast and that I cannot create a non-static member function stream.

I cannot make my static function because I am using this pointer multiple times, which requires a non-static member function.

Is there an easy way to create a stream of a non-static member function?

(I am working in Visual Studio 2010, C ++, MFC)

+3


source to share


3 answers


You should privatize all of this information, with the possible exception of the "start ()" function. You will have a private static member function of your class as the target to start the operating system thread, in "start ()" pass the thread start method the "this" pointer of your object, cast it back to the object type in your static function, which then calls your own main thread method on the object itself. Since a static function is a member of a class, it can use private functions, whereas if it weren't, it couldn't (not being a friend). I haven't compiled / tested this, but the idea is this:

class MyObj {
private:
    void thread() {
            // this-> is valid here
    }

    static DWORD static_entry(LPVOID* param) {
        MyObj *myObj = (MyObj*)parm;
        myObj->thread();
        return 0;
    }

public:
    void start() {
        CreateThread(Null, 0, (LPTHREAD_START_ROUTINE)static_entry, this, 0, NULL);
    }
};

      



A word of warning: don't destroy the object while the thread is running! You may need to sync with the mutex or make sure the thread is connected when the object is destroyed. You can also do "delete this" at the end of the stream (), or delete myObj at the end of static_entry if the caller start () no longer controls the object.

+4


source


A C ++ member function cannot be called without an instance of the class. The following code is one of my suggestions;

MyClass * instance = ...; // valid instance.
CreateThread(NULL, 0, StartRoutine, instance, 0, NULL);

DWORD WINAPI StartRoutine(LPVOID ptr) {
  static_cast<MyClass*>(ptr)->MyFunction();
}

      

Another suggestion is to declare a member function with a keyword static

;



class MyClass {
  ...
  static DWORD WINAPI MyFunction();
  ...
}

      

The second code MyFunction

cannot access the member variables of the class (not static

).

+2


source


Create static function

static DWORD myFunctionCaller(LPVOID* param)  
{
  MyClass* myClass = static_cast<MyClass*>(param);
  myClass->MyFunction();
}

CreateThread(Null, 0, (LPTHREAD_START_ROUTINE)myFunctionCaller, this, 0, NULL);

      

The downside is that you get multiple floating static functions, but you can easily constrain the scope and this should prevent you from creating too many threads

+2


source







All Articles