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

Loren Osborn linux_dr at yahoo.com
Tue Jul 23 13:06:01 PDT 2002

--- Huib-Jan Imbens <hj at imbens.nl> wrote:
> I don't want the thread to quit. It is doing a
> lengthy calculation for me
> and it quits when it has finished. It would have
> been easiest for me if
> there were some function int SDL_ThreadStatus
> (SDLThread *thread) that
> allows me to get the status of the thread without
> having to add a global
> flag and a mutex to protect that flag.

1) If you *DON'T* want the thread to quit, have it do
something else instead of quit (i.e. have it set a
flag asking the main thread for more stuff to

2) To determine if the a thread is still running, you
actually shouldn't NEED a mutex:  There is one point
in time where the thread will announce it is about to
exit.  around that time, the main thread has no
buisness WRITING to that flag, also that flag does not
need to be kept coherent with anything else, so it
that scenario, there is (strictly speaking) no need
for the mutex. Mutexes are primarily needed when the
direction of communications are in question, or  when
the data being shared by multiple threads can exist in
an undefined state.  Bare in mind that with one-way
sequential guarenteed data between two threads,
two-way communication (and therefore mutexes) are
probably required.

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;

bool IsThreadStillRunning()
  return (!IsCalculateThreadAboutToExit); 

Best Regards,


Do You Yahoo!?
Yahoo! Health - Feel better, live better

More information about the SDL mailing list