SDL thread problem

Hi Folks

Got a strange problem using threads under SDL…

Basically, once I create the thread, the thread-function gets called once (on creation) and never again.

Any ideas? I’m using MS VC++ 2005 and it’s set for multi-threaded DLL - and I’m just totally confused…

Many Thanks for any advice…

Ed___________________________________________________________
Yahoo! Mail is the world’s favourite email. Don’t settle for less, sign up for
your free account today http://uk.rd.yahoo.com/evt=44106/*http://uk.docs.yahoo.com/mail/winter07.html

Why, yes…? That’s how it’s supposed to work. :slight_smile: The thread is
created and scheduled to run, and then control returns to the main
thread at the same time (literally, if you’re on a
multicore/multiprocessor machine) as the thread function is executed.

The thread ends when the thread function returns. (It’s possible to
kill threads from the outside too, but doing so is asking for
trouble. Tell the threads to stop and let them clean up and
end “naturally” whenever possible.)

BTW, this is not really SDL specific. This is pretty much how threads
work, regardless of platforms and APIs.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --'On Wednesday 23 May 2007, Edward Byard wrote:

Hi Folks

Got a strange problem using threads under SDL…

Basically, once I create the thread, the thread-function gets called
once (on creation) and never again.

When you create the thread using SDL_CreateThread(myThreadFunc) it will
fire up a LWP to run the user function myThreadFunc ONCE and once only…

From this point on it’s up to your thread function do do whatever it is
it has to do , either in a loop or exit.

I think your getting confused with SDL timers.
-Chris________________________________

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org]
On Behalf Of Edward Byard
Sent: 23 May 2007 14:11
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: [SDL] SDL thread problem

Hi Folks

Got a strange problem using threads under SDL…

Basically, once I create the thread, the thread-function gets called
once (on creation) and never again.

Any ideas? I’m using MS VC++ 2005 and it’s set for multi-threaded DLL -
and I’m just totally confused…

Many Thanks for any advice…

Ed


Yahoo! Answers - Got a question? Someone out there knows the answer. Try
it now
<http://uk.answers.yahoo.com/;_ylc=X3oDMTEzaDRkZGdlBF9TAzIxMTQ3MTcxOTAEc
2VjA01haWwEc2xrA3RhZ2xpbmVz> .

Basically, once I create the thread, the thread-function gets called once
(on creation) and never again.

Not quite sure what the problem is here…That’s exactly what’s meant to
happen.
Say you have:

int f(void* data)
{
printf(“foo\n”);
}

and then you do something like:

int main()
{
void *data=0;
SDL_Thread *t = SDL_CreateThread(f, data);
SDL_WaitThread(t, 0);
return 0;
}

The output will be:

foo

Am I misunderstanding your problem? BTW, if you’re new to threads, it will
save you LOADS of trouble in the long run to read
"Programming with POSIX Threads" by David Butenhof.

Cheers,
Kos_____

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Edward Byard
Sent: 23 May 2007 14:11
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: [SDL] SDL thread problem

Hi Folks

Got a strange problem using threads under SDL…

Basically, once I create the thread, the thread-function gets called once
(on creation) and never again.

Any ideas? I’m using MS VC++ 2005 and it’s set for multi-threaded DLL - and
I’m just totally confused…

Many Thanks for any advice…

Ed


Yahoo! Answers - Got a question? Someone out there knows the answer. Try
<http://uk.answers.yahoo.com/;_ylc=X3oDMTEzaDRkZGdlBF9TAzIxMTQ3MTcxOTAEc2VjA
01haWwEc2xrA3RhZ2xpbmVz> it now.

Stupid me.

That’s what you get when you’re used to embedded systems (no OS) and just expect a thread to run in the background until you stop it (read: an interrupt function).

So I need to have a loop in my thread, I guess…and just do stuff in there until it needs to quit.

Thanks
Ed> ----- Original Message -----

From: david@olofson.net (David Olofson)
To: A list for developers using the SDL library. (includes SDL-announce)
Sent: Wednesday, 23 May, 2007 2:30:45 PM
Subject: Re: [SDL] SDL thread problem

On Wednesday 23 May 2007, Edward Byard wrote:

Hi Folks

Got a strange problem using threads under SDL…

Basically, once I create the thread, the thread-function gets called
once (on creation) and never again.

Why, yes…? That’s how it’s supposed to work. :slight_smile: The thread is
created and scheduled to run, and then control returns to the main
thread at the same time (literally, if you’re on a
multicore/multiprocessor machine) as the thread function is executed.

The thread ends when the thread function returns. (It’s possible to
kill threads from the outside too, but doing so is asking for
trouble. Tell the threads to stop and let them clean up and
end “naturally” whenever possible.)

BTW, this is not really SDL specific. This is pretty much how threads
work, regardless of platforms and APIs.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --’


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


Copy addresses and emails from any email account to Yahoo! Mail - quick, easy and free. http://uk.docs.yahoo.com/trueswitch2.html