Jump to content
  • Advertisement
Sign in to follow this  
cignox1

passing a member function to SDL_CreateThread()

This topic is 5400 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

Hi, I'm writing a simple application using SDL (for the first time) and I've done a stupid designing error: I wrapped my data in classes (C++) and some of these should run in a separate thread. Now, the problem is that I cannot pass a method of these class as the parameter of SDL_CreateThread(). Is there a way to cast a pointer to a member function and get it work? I simply could add a function that will be called by SDL_CreateThread(), and then this function calls the correct member of the correct object, but I wonder if is there a more elegant way to do this... Thank you!

Share this post


Link to post
Share on other sites
Advertisement
If SDL_CreateThread() lets you pass a user parameter (which most functions like this do, not sure about SDL though), then you can do this:

class CClass
{
protected:
static void StaticThreadProc(void* pParam)
{
return ((CClass*)pParam)->ThreadProc();
}

void ThreadProc() {/* ... */}
};

// In your code (in some member function of CClass):
SDL_CreateThread(StaticThreadProc,this);


I.e. call a static member function that then calls the non-static member. This is a lot neater since you don't need to worry about havign a global object, and a public ThreadProc().

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!