How to avoid running out of OS thread handles when spawning lots of short-lived threads?

Steven Schveighoffer schveiguy at yahoo.com
Thu Jun 9 16:37:39 PDT 2011


On Thu, 09 Jun 2011 19:17:28 -0400, David Nadlinger <see at klickverbot.at>  
wrote:

> The title says it all – how can I avoid running out of OS thread handles  
> when spawning lots of short-lived threads?
>
> In reality, I encountered the issue while writing tests a piece of code  
> which spawns a thread, but this is the basic issue:
>
> ---
> import core.thread;
>
> void doNothing() {}
> void main() {
>    foreach (i; 0 .. 100_000) {
>      auto t = new Thread(&doNothing);
>      t.start();
>
>      // Just to make sure the thread has time to terminate.
>      Thread.sleep(dur!"msecs"(1));
>    }
> }
> ---
>
> Even though the threads immediately terminate, the D Thread objects stay  
> around a lot longer (until the garbage collector decides to collect  
> them), and as pthread_detach is only called the Thread destructor, this  
> causes the application to eventually fail with
>
> core.thread.ThreadException at src/core/thread.d(812): Error creating thread
>
> because the available OS thread handles are exhausted.
>
> Any ideas how to properly fix that?

t.join() ?

http://www.digitalmars.com/d/2.0/phobos/core_thread.html#join

AFAIK, a thread cannot go away until you join it, because it still has to  
give you its exit status.

See this man page:  
http://pubs.opengroup.org/onlinepubs/007908799/xsh/pthread_join.html

It is unspecified whether a thread that has exited but remains unjoined  
counts against _POSIX_THREAD_THREADS_MAX.

-Steve


More information about the Digitalmars-d-learn mailing list