[SDL] Re: SDL_CreateThread

Rainer Deyke root at rainerdeyke.com
Tue Jul 16 21:01:00 PDT 2002

Nick Groesz wrote:
> My particular problem is that I have 4 member functions within my
> client class to be threaded and 4 member functions in my server
> that I want to run in threads.
> This would require a bunch of global functions that I would rather
> pollute my code with. I am hoping someone here has a better idea as
> how to get around this problem. My apologies if this is more of a
> C/C++ programming question.

If you have lots of functions that look identical, you can often
generate them automatically with templates:

template<int (MyClass::* fn)()> class MyClassFunction {
  static int run(void *it)
     return (static_cast<MyClass *>(it))->*fn();

SDL_CreateThread(MyClassFunction<MyClass::myFunction>::run, (void

Another is to pass the member functions pointer to the callback
(although this is somewhat difficult because of memory management

typedef int (MyClass::*member_function_t)();
typedef std::pair<member_function_t, MyClass *> callback_data_t;

int callback(void *raw_data)
  callback_data_t *data = static_cast<callback_data_t *>(raw_data);
  return data->second->(*data->first)();

call_back_data_t callback_data = std::make_pair(MyClass::myFunction,
SDL_CreateThread(callback, (void *)(&callback_data));

Rainer Deyke | root at rainerdeyke.com | http://rainerdeyke.com

More information about the SDL mailing list