• Advertisement
Sign in to follow this  

SDL_CreateThread() and Memberfunctions

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

Im trying to use SDL_CreateThread() in order to have multiple threads running in my game at once. I noticed that there is a problem with calling a member function of a class, seems like SDL_CreateThread only likes global functions.... Is there a way to use some thing like
void Server::server_thread(void* data)
{
   ...
}
as a function for a SDL Thread? Also, can I somehow use namespaces, for instance,
server::Server::server_thread()
as thread function?

Share this post


Link to post
Share on other sites
Advertisement
There's a search box on pretty much every forum page in the upper right. Try entering "SDL_CreateThread member function" there.

Share this post


Link to post
Share on other sites
It is fairly easy. You can use the void pointer to pass the "this" pointer:

namespace
{
int threadProxy( void *pointer )
{
try
{
Thread &thread = *static_cast<Thread*>(pointer);
thread.run();
return 0;
}
catch( const std::exception &e )
{
std::cerr << e.what() << std::endl;
return 1;
}
catch( ... )
{
std::cerr << "Unknown object thrown\n";
return 2;
}
}
}



And usage:

void Thread::start()
{
assert(!thread);
thread = SDL_CreateThread(&threadProxy, this);
if(!thread)
{
throw std::runtime_error(SDL_GetError());
}
}



In this example, Thread::run() is a virtual function that does whatever processing the thread wants to do. While this example forces you to derive from a thread, it wouldn't be hard to write a generic wrapper class that turns any member function with the right signature into a thread.

Share this post


Link to post
Share on other sites
First of all, thank you both for your replies.

If I get rip-offs idea right, I'll create a class Thread which has the thread_proxy() and run() functions, and every class which has a thread has to derive from it.

Do I get this right so far? And has the thread_proxy function to be in a nameless namespace?

Share this post


Link to post
Share on other sites
Quote:
Original post by Sceletor
If I get rip-offs idea right, I'll create a class Thread which has the thread_proxy() and run() functions, and every class which has a thread has to derive from it.


Well, thread_proxy() would have to be static if it is part of a class. But yes, that is one way of doing it. However, there is no particular reason a task has to derive from a thread. Example:

struct Task
{
virtual ~Task(){}
virtual void run() = 0;
};



Then you pass a task (smart) pointer to a thread's constructor, and call Task::run on that. With some template and/or boost hackery you could generalise the concept of a task such that any member function on any object could be made into a task and run in a thread.

void Thread::start()
{
assert(!thread);
thread = SDL_CreateThread(&threadProxy, task);
if(!thread)
{
throw std::runtime_error(SDL_GetError());
}
}




Quote:

And has the thread_proxy function to be in a nameless namespace?


No, it doesn't. I would chose to have it that way because it is an implementation detail inside a source file and shouldn't be called manually by client code.

Share this post


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

  • Advertisement