Running a member method on a worker thread...

Started by
5 comments, last by Saruman 18 years, 10 months ago
Is it possible to launch a member method on a worker thread from another member method of the same class? Say I have the following two methods that belong to the StatisticScreen class.

HRESULT StatisticScreen::StartScreen();

DWORD StatisticScreen::PrintingThread(void);

HRESULT StatisticScreen::StartScreen()
{
     // ?????????????? Is this legal ????????????????
     // Start PrintingThread(void) on another thread
     DWORD dwThreadID1;
     HANDLE Handle1 = CreateThread (NULL, 0, PrintingThread, NULL, 0, &dwThreadID1);

     return S_OK;
}

If this is not the correct method, is there anyway to achieve this? Please don't flame me for not trying it out first, I'm just trying to save myself time and effort! Kind regards, Mark Coleman
Advertisement
That will only work if PrintingThread is declared as static.
Thanks Dave, I'll try that out now.

Mark Coleman
The classical solution is to use the LPVOID parameter to pass a pointer to this to a static method:

class Thread{  static LRESULT static_threadfunc(LPVOID param);  LRESULT threadfunc();  DWORD mThreadId;  HANDLE mHThread;public:  Thread();};Thread::Thread(){  mHThread = CreateThread(NULL, 0, static_threadfunc, (LPVOID)this, 0, &mThreadId);}LRESULT Thread::static_threadfunc(LPVOID param){  Thread *thread = (Thread*)param;  return thread->threadfunc();}LRESULT Thread::threadfunc(){  // ...} 


When you create a new thread, it knows to which instance it belongs; he static function is then able to call a member function of this instance.

HTH,
This doesn't belong to the "DirectX forum".

It isn't in the DirectX forum.
Quote:Original post by mrmrcoleman
It isn't in the DirectX forum.

It probably was before he moved it :)

This topic is closed to new replies.

Advertisement