Many SDL_CreateThread()s

Could there be any system stability or performance issues if I’m calling SDL_CreateThread() once or twice every thirty seconds (and the threads end at similar frequencies so that there are never more than four in addition to the long term main threads)?? I’m interested in this running on both Windows and Linux.

As far as I know, there’s no problem as long as the threads clean up after themselves properly. I had a program that created and recycled threads a lot more frequently than that, and it didn’t break anything. (Of course, it wasn’t using SDL threads, but the basic principles are sound. Unless there’s some sort of flaw in SDL’s threading code, you’ll be fine.)________________________________
From: bdt@shaw.ca (Borislav Trifonov)
To: sdl at lists.libsdl.org
Sent: Tuesday, October 28, 2008 7:50:22 PM
Subject: [SDL] Many SDL_CreateThread()s

Could there be any system stability or performance issues if I’m calling SDL_CreateThread() once or twice every thirty seconds (and the threads end at similar frequencies so that there are never more than four in addition to the long term main threads)? I’m interested in this running on both Windows and Linux.

I believe you would need to call SDL_WaitThread() to totally clean up
the memory used by the thread. If memory serves SDL allocates some
memory per thread too, which is only cleaned up when WaitThread() is
called. Because WaitThread() doesn’t support timeouts, your threads
will need to signal their parent that they want collecting. For
example, if the main() thread is the parent, the children could use
SDL_PushEvent() on a SDL_USEREVENT to alert main() to collect them.

Actually, I’m not even sure that the parent thread has to be the one
to call WaitThread() on them.

That said, I imagine it might be more efficient if you could request
threads to sleep (using a threading primitive) instead of ending them,
and wake them up periodically. Depending on the operating system, I
imagine thread creation could be quite expensive.On Wed, Oct 29, 2008 at 2:50 AM, Borislav Trifonov wrote:

Could there be any system stability or performance issues if I’m calling
SDL_CreateThread() once or twice every thirty seconds (and the threads end
at similar frequencies so that there are never more than four in addition to
the long term main threads)? I’m interested in this running on both Windows
and Linux.

I believe you would need to call SDL_WaitThread() to totally clean up
the memory used by the thread. If memory serves SDL allocates some
memory per thread too, which is only cleaned up when WaitThread() is
called. Because WaitThread() doesn’t support timeouts, your threads
will need to signal their parent that they want collecting. For
example, if the main() thread is the parent, the children could use
SDL_PushEvent() on a SDL_USEREVENT to alert main() to collect them.

Actually, I’m not even sure that the parent thread has to be the one
to call WaitThread() on them.

That said, I imagine it might be more efficient if you could request
threads to sleep (using a threading primitive) instead of ending them,
and wake them up periodically. Depending on the operating system, I
imagine thread creation could be quite expensive.

Thread creation can be expensive. The usual solution is something like
what you suggested, but not quite. It is called a thread pool. You
create a small set of threads and have then wait on a condition
variable. When you want a thread you send the signal and essentially
let one thread out of the corral to do the job. When it is done it
just awaits the signal again.

The code that releases a thread from the pool can first check to see
if there is a thread waiting for work. If not, it can create a new
thread. Doing it that way lets you start out with no threads and adds
new threads only when/if they are needed. That minimizes the creation
and deletion of threads.

For more information google “thread pool”. There is a lot of info out
there, even code to do it for you. In SDL look at the
SDL_CreateCond(), SDL_CondWait(), and SDL_CondSignal().

Bob PendletonOn Wed, Oct 29, 2008 at 7:03 AM, Brian <brian.ripoff at gmail.com> wrote:

On Wed, Oct 29, 2008 at 2:50 AM, Borislav Trifonov wrote:

Could there be any system stability or performance issues if I’m calling
SDL_CreateThread() once or twice every thirty seconds (and the threads end
at similar frequencies so that there are never more than four in addition to
the long term main threads)? I’m interested in this running on both Windows
and Linux.


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

±-------------------------------------+