Jump to content
  • Advertisement
Sign in to follow this  
mrmrcoleman

Running a member method on a worker thread...

This topic is 4733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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,

Share this post


Link to post
Share on other sites
Quote:
Original post by mrmrcoleman
It isn't in the DirectX forum.

It probably was before he moved it :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!