Classes and Threads

Apokliptico wrote:

Hello, I’m kinda about to throw the computer out the window because this is really frustrating…
Could please, for the love of thor someone show me a working example of SDL_Thread working as a class member on SDL 2.0??

Hi, I recognize that feeling!
My answer may drive you even more crazy, but you cannot render something from a separate thread. What you’ill have to do is sending a message to the main thread, asking to render something. To my knowledge there is no support for messages in SDL, only some building blocks (e.g. mutexes) are provided. You will have to build a message queue yourself. Thread programming is not very difficult, it is described in books and on the internet. Googling “thread programming tutorial” would be a good start.

SDL already has an event queue that you can use for messaging. You just use a custom event type.________________________________
From: wboe <w.boeke at upcmail.nl>
To: sdl at lists.libsdl.org
Sent: Sunday, February 24, 2013 8:55 AM
Subject: Re: [SDL] Classes and Threads.

Apokliptico wrote:

Quote:
Hello, I’m kinda about to throw the computer out the window because this is really frustrating…
Could please, for the love of thor someone show me a working example of SDL_Thread working as a class member on SDL 2.0??

Hi, I recognize that feeling!
My answer may drive you even more crazy, but you cannot render something from a separate thread. What you’ill have to do is sending a message to the main thread, asking to render something. To my knowledge there is no support for messages in SDL, only some building blocks (e.g. mutexes) are provided. You will have to build a message queue yourself. Thread programming is not very difficult, it is described in books and on the internet. Googling “thread programming tutorial” would be a good start.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi Apokliptico,

I don’t know what you’ve tried, but it’s probably a little simpler than you
thought, however, we’ll have to do a little mangling with a static function

class SomeClassWithAThread

{
public:
SomeClassWithAThread()
: myThread( NULL )
{
}
~SomeClassWithAThread()
{
if ( myThread ) Join();
}
bool IsRunning( void )
{
return isRunning;
}
void Run( void )
{
if ( isRunning ) return; // May want to throw an error if you try and
run the thread while it’s already running?
myThread = SDL_CreateThread( SomeClassWithAThread::ThreadFunction,
NULL, (void *)this );
}
int Join( void )
{
int returnValue = 0;
if ( isRunning )
{
isRunning = false;
SDL_WaitThread( myThread, &returnValue );
myThread = NULL;
}
return returnValue;
}
private:
static int ThreadFunction( void *param ) // Doesn’t have to be private,
BUT to run as a thread, it does need to be static
{
SomeClassWithAThread *self = (SomeClassWithAThread *)param;
while ( self->IsRunning() )
{
SDL_Delay( 10 ); // Put whatever code you want here. Make sure you
use SDL_Mutex if you’re doing lots of global/member variable alteration.
}
}
volatile bool isRunning; // For something basic like a bool, setting
volatile is probably overkill, but it just tells the compiler that this
variable may be modified by more than one thread at the same time.
SDL_Thread *myThread;
};

Hopefully that helps clear things up, and if not, let us know.On Sun, Feb 24, 2013 at 11:55 AM, wboe <w.boeke at upcmail.nl> wrote:

**
Apokliptico wrote:

Quote:

Hello, I’m kinda about to throw the computer out the window because this
is really frustrating…
Could please, for the love of thor someone show me a working example of
SDL_Thread working as a class member on SDL 2.0??

Hi, I recognize that feeling!
My answer may drive you even more crazy, but you cannot render something
from a separate thread. What you’ill have to do is sending a message to the
main thread, asking to render something. To my knowledge there is no
support for messages in SDL, only some building blocks (e.g. mutexes) are
provided. You will have to build a message queue yourself. Thread
programming is not very difficult, it is described in books and on the
internet. Googling “thread programming tutorial” would be a good start.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Actually, SDL does have messages. The SDL events, typically used for input,
are thread safe, and you can add user defined event types for this kind of
stuff.

SDL 1.2 had a fair bit of globally shared internal state, and not even pure
software rendering into independent surfaces was thread safe. I’m not sure
if these limitations still stand with SDL2.On Sun, Feb 24, 2013 at 5:55 PM, wboe <w.boeke at upcmail.nl> wrote:

**
Apokliptico wrote:

Quote:

Hello, I’m kinda about to throw the computer out the window because this
is really frustrating…
Could please, for the love of thor someone show me a working example of
SDL_Thread working as a class member on SDL 2.0??

Hi, I recognize that feeling!
My answer may drive you even more crazy, but you cannot render something
from a separate thread. What you’ill have to do is sending a message to the
main thread, asking to render something. To my knowledge there is no
support for messages in SDL, only some building blocks (e.g. mutexes) are
provided. You will have to build a message queue yourself. Thread
programming is not very difficult, it is described in books and on the
internet. Googling “thread programming tutorial” would be a good start.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’

Ah, I missed the original message, so sorry that I didn’t answer as fully
as these other gents.

I am under the impression that if you create a window in a separate thread,
it is possible to do any of it’s rendering inside that thread, but not in
any other place.

This is typically not an ideal approach, because it adds unnecessary
complexity into your code. You may find doing some simple processing in a
separate thread is acceptable, and then sending any messages produced to
the SDL event queue to be more acceptable, as others have suggested.

Also, what sort of errors were you getting?On Sun, Feb 24, 2013 at 12:10 PM, Alex Barry <@Alex_Barry> wrote:

Hi Apokliptico,

I don’t know what you’ve tried, but it’s probably a little simpler than
you thought, however, we’ll have to do a little mangling with a static
function

class SomeClassWithAThread

{
public:
SomeClassWithAThread()
: myThread( NULL )
{
}
~SomeClassWithAThread()
{
if ( myThread ) Join();
}
bool IsRunning( void )
{
return isRunning;
}
void Run( void )
{
if ( isRunning ) return; // May want to throw an error if you try and
run the thread while it’s already running?
myThread = SDL_CreateThread( SomeClassWithAThread::ThreadFunction,
NULL, (void *)this );
}
int Join( void )
{
int returnValue = 0;
if ( isRunning )
{
isRunning = false;
SDL_WaitThread( myThread, &returnValue );
myThread = NULL;
}
return returnValue;
}
private:
static int ThreadFunction( void *param ) // Doesn’t have to be private,
BUT to run as a thread, it does need to be static
{
SomeClassWithAThread *self = (SomeClassWithAThread *)param;
while ( self->IsRunning() )
{
SDL_Delay( 10 ); // Put whatever code you want here. Make sure you
use SDL_Mutex if you’re doing lots of global/member variable alteration.
}
}
volatile bool isRunning; // For something basic like a bool, setting
volatile is probably overkill, but it just tells the compiler that this
variable may be modified by more than one thread at the same time.
SDL_Thread *myThread;
};

Hopefully that helps clear things up, and if not, let us know.

On Sun, Feb 24, 2013 at 11:55 AM, wboe <w.boeke at upcmail.nl> wrote:

**
Apokliptico wrote:

Quote:

Hello, I’m kinda about to throw the computer out the window because
this is really frustrating…
Could please, for the love of thor someone show me a working example of
SDL_Thread working as a class member on SDL 2.0??

Hi, I recognize that feeling!
My answer may drive you even more crazy, but you cannot render something
from a separate thread. What you’ill have to do is sending a message to the
main thread, asking to render something. To my knowledge there is no
support for messages in SDL, only some building blocks (e.g. mutexes) are
provided. You will have to build a message queue yourself. Thread
programming is not very difficult, it is described in books and on the
internet. Googling “thread programming tutorial” would be a good start.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Also, in my Run() function, I forgot the last line:

if ( myThread != NULL ) isRunning = true;On Sun, Feb 24, 2013 at 12:17 PM, Alex Barry <@Alex_Barry> wrote:

Ah, I missed the original message, so sorry that I didn’t answer as fully
as these other gents.

I am under the impression that if you create a window in a separate
thread, it is possible to do any of it’s rendering inside that thread, but
not in any other place.

This is typically not an ideal approach, because it adds unnecessary
complexity into your code. You may find doing some simple processing in a
separate thread is acceptable, and then sending any messages produced to
the SDL event queue to be more acceptable, as others have suggested.

Also, what sort of errors were you getting?

On Sun, Feb 24, 2013 at 12:10 PM, Alex Barry <@Alex_Barry> wrote:

Hi Apokliptico,

I don’t know what you’ve tried, but it’s probably a little simpler than
you thought, however, we’ll have to do a little mangling with a static
function

class SomeClassWithAThread

{
public:
SomeClassWithAThread()
: myThread( NULL )
{
}
~SomeClassWithAThread()
{
if ( myThread ) Join();
}
bool IsRunning( void )
{
return isRunning;
}
void Run( void )
{
if ( isRunning ) return; // May want to throw an error if you try
and run the thread while it’s already running?
myThread = SDL_CreateThread( SomeClassWithAThread::ThreadFunction,
NULL, (void *)this );
}
int Join( void )
{
int returnValue = 0;
if ( isRunning )
{
isRunning = false;
SDL_WaitThread( myThread, &returnValue );
myThread = NULL;
}
return returnValue;
}
private:
static int ThreadFunction( void *param ) // Doesn’t have to be
private, BUT to run as a thread, it does need to be static
{
SomeClassWithAThread *self = (SomeClassWithAThread *)param;
while ( self->IsRunning() )
{
SDL_Delay( 10 ); // Put whatever code you want here. Make sure
you use SDL_Mutex if you’re doing lots of global/member variable alteration.
}
}
volatile bool isRunning; // For something basic like a bool, setting
volatile is probably overkill, but it just tells the compiler that this
variable may be modified by more than one thread at the same time.
SDL_Thread *myThread;
};

Hopefully that helps clear things up, and if not, let us know.

On Sun, Feb 24, 2013 at 11:55 AM, wboe <w.boeke at upcmail.nl> wrote:

**
Apokliptico wrote:

Quote:

Hello, I’m kinda about to throw the computer out the window because
this is really frustrating…
Could please, for the love of thor someone show me a working example of
SDL_Thread working as a class member on SDL 2.0??

Hi, I recognize that feeling!
My answer may drive you even more crazy, but you cannot render something
from a separate thread. What you’ill have to do is sending a message to the
main thread, asking to render something. To my knowledge there is no
support for messages in SDL, only some building blocks (e.g. mutexes) are
provided. You will have to build a message queue yourself. Thread
programming is not very difficult, it is described in books and on the
internet. Googling “thread programming tutorial” would be a good start.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org