How to avoid running out of OS thread handles when spawning lots of short-lived threads?
David Nadlinger
see at klickverbot.at
Thu Jun 9 16:17:28 PDT 2011
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?
David
More information about the Digitalmars-d-learn
mailing list