[SDL] how to detect if a thread is still running

Loren Osborn linux_dr at yahoo.com
Tue Jul 23 15:01:02 PDT 2002


--- Neil Bradley <nb at synthcom.com> wrote:
> > For a mutex-less exit status, the code should look
> > something like this:
> > bool IsCalculateThreadAboutToExit = true;
> > int StartCalculateThread(void *)
> > {
> >   uint i;
> >   for(i=0; i<1000000; i++)
> >   {
> >     /* Calculate Stuff */
> >   }
> >   /* I'm about to exit: tell the world! */
> >   IsCalculateThreadAboutToExit = true;
> > }
> >
> > void LaunchCalculateThread()
> > {
> >   IsCalculateThreadAboutToExit = false;
> >   SDL_CreateThread(StartCalculateThread);
> > }
> >
> > bool IsThreadStillRunning()
> > {
> >   return (!IsCalculateThreadAboutToExit);
> > }
> 
> I realize this is only an example, but with the
> "IsThreadStillRunning"
> procedure, I feel compelled to say that that'd peg
> the processor at 100%
> utilization.

Umm... IsThreadStillRunning() is simply an accessor. 
It doesn't loop, or anything like that... The user
wanted to know "is the other thread still running"...
said nothing about WHEN he was quereing the status of
the thread or HOW OFTEN.  Admittedly, this function
does have to be quereied, but it's stupid to assume
that the main thread won't be doing something else, or
that the latency required must be so low that the main
thread must busy-wait on it's value.  There are better
 methods than polling a variable, but most games (SDL
is used primarily for games) have main-loops, and
these main loops can be used for this type of polling
in situations that don't require low latency. 
Typically threads should not be used where extremely
low latency is required.

> Andi t begs the question, if a
> subordinate thread is spawned
> off, only to be waited on by another thread, why was
> the subordinate
> thread created in the first place?

Who said anything about what the main thread was
doing?

> 
> There are very few reasons to create another thread
> for something, and
> more often I see thread abuse than use. It might be
> tempting (whizzy/cool)
> to use a thread, but there are so few circumstances
> when it's actually
> needed that when someone says they need a thread for
> something, more often
> than not they don't!

I agree that I have seen threads abused in many
instances, but they do have their place... Especially
for asynchronous data processing.

> 
> -->Neil
> 
> 

Cheer,

-Loren

__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com




More information about the SDL mailing list