Threads and classes

I’m quite new to this threads lark, so you’ll have to excuse me if this
sounds like a daft question.

I’ve got a game which I’d like to split up into several threads, however
my game engine is very class orientated. I was trying to run one of the
class methods as a seperate thread. From what I can see the
SDL_CreateThread() function won’t accept a class method.

What’s the best way around this? Is it really bad practice of something
to put parts of classes into seperate threads? Should I make the said
method a friend function?

I can see I’m going to running into this problem quite a bit, since
I have modulised nearly all of my game into classes…

Arthur.

Keep an instance of that object around in a global variable and start the
thread with a public function that calls the class’s method on the global
instance. Make sure you protect your objects from race conditions.–

Olivier A. Dagenais - Software Architect and Developer

“Arthur J. Yarwood” wrote in message
news:9797ki$jdu$1 at ftp.lokigames.com

I’m quite new to this threads lark, so you’ll have to excuse me if this
sounds like a daft question.

I’ve got a game which I’d like to split up into several threads, however
my game engine is very class orientated. I was trying to run one of the
class methods as a seperate thread. From what I can see the
SDL_CreateThread() function won’t accept a class method.

What’s the best way around this? Is it really bad practice of something
to put parts of classes into seperate threads? Should I make the said
method a friend function?

I can see I’m going to running into this problem quite a bit, since
I have modulised nearly all of my game into classes…

Arthur.

“Arthur J. Yarwood” wrote:

I’m quite new to this threads lark, so you’ll have to excuse me if this
sounds like a daft question.

I’ve got a game which I’d like to split up into several threads, however
my game engine is very class orientated. I was trying to run one of the
class methods as a seperate thread. From what I can see the
SDL_CreateThread() function won’t accept a class method.

What’s the best way around this? Is it really bad practice of something
to put parts of classes into seperate threads? Should I make the said
method a friend function?

I can see I’m going to running into this problem quite a bit, since
I have modulised nearly all of my game into classes…

Arthur.

Here is an over simplification of how to make a thread class. If you
want to see a more complete example, download libksd.

Make a static functions along the lines of:

int ThreadCallback(void* tptr)
{

MyThreadClass* thread = (MyThreadClass*)tptr;
thread->Run();  // pure virual thread function.

}

And in the thread class:

void MyThreadClass::Start()
{
SDL_CreateThread(&ThreadCallback, this);
}

-- David Snopek

/-- libksd –
| The C++ Cross-Platform Game Framework
| Only want to write it once??
| http://libksd.sourceforge.net
------------

David Snopek wrote:

Here is an over simplification of how to make a thread class. If you
want to see a more complete example, download libksd.

Make a static functions along the lines of:

int ThreadCallback(void* tptr)
{

    MyThreadClass* thread = (MyThreadClass*)tptr;
    thread->Run();  // pure virual thread function.

}

And in the thread class:

void MyThreadClass::Start()
{
SDL_CreateThread(&ThreadCallback, this);
}

    -- David Snopek

Stunning. That’s almost letter-perfect from my own code! The
first character of methods is lower case in my code, and
I use threadCallback(void *ptr), but that’s it!

Simon.–
Freedom ? What’s that ? (see http://www.domesday.co.uk/ )