Hi.
Is it possible to pass a pointer to a class member function to a worker thread?
Otherwise, is it possible to pass in a global function? If yes, do you have to use typedef (*func)(parameters)?
Thanks,
Kuphryn
Hi.
Is it possible to pass a pointer to a class member function to a worker thread?
Otherwise, is it possible to pass in a global function? If yes, do you have to use typedef (*func)(parameters)?
Thanks,
Kuphryn
Do you mean the
AfxBeginThread type of worker thread? or do you mean the _beginthread type of thread????
All Thread Function for the AfxBeginThread must be decalared like this:
However, you can name it whatever you want pass a pointer that is cast to your object and the function can be modifed to take a pointer to your object.Code:UINT ThreadProc(VOID * pVoid)
This is the call the to start the thread:Code:UINT MyThreadProc(MyOject * pMyObject);
Inside the MyThreadProc you can access that object's methods. A pointer to any method may be possible of you could figure out how to use the pointer at the other end.Code:CWinThread * pThread; MyObject * p_MyObject; pThread = AfxBeginThread( (AFX_THREADPROC)MyThreadProc, (MyObject *)p_MyObject );
Okay. Thanks.
I believe you meant passing a function to CreateThread() or _beginThread(). I know that is possible. In fact, that is a necessity if you want to call a function to do work.
I am interested more in passing in the thread a function pointer. In other words, I want to call a function while inside the thread.
Kuphryn
DrPizza of GameDev brought up a solution I missed yesterday. The solution is to encapsulate a function in a struct that is a class member. That will allow you go not have to declare a global function.
The only thing I am uncertain of is if you have a private member struct, will the global worker thread function i.e. the function you pass to __BeginThread() recognize the abstract private member.
Kuphryn