passing a member function to SDL_CreateThread()

Started by
1 comment, last by cignox1 19 years, 6 months ago
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!
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().
Very nice solution, thank you!

(...rating you up....;-)

This topic is closed to new replies.

Advertisement