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@src/core/thread.d(812): Error creating thread

because the available OS thread handles are exhausted.

Any ideas how to properly fix that?

David

Reply via email to