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:57:27 PDT 2011


On 6/10/11 1:37 AM, Steven Schveighoffer wrote:
> t.join() ?
>
> http://www.digitalmars.com/d/2.0/phobos/core_thread.html#join
Doesn't work, in my application I'm a) using std.concurrency, and b) 
even that is hidden behind the API I want to test. A better example 
would probably be the following, which crashes before reaching 600 
iterations on my Arch Linux VM:

---
import std.concurrency;

void doNothing() {}
void main() {
   foreach (i; 0 .. 1_000_000) {
     spawnLinked(&doNothing);
     receive((LinkTerminated t){});
   }
}
---

I realize that spawn()ing lots of threads without causing much GC 
activity may be an edge case that probably only occurs during testing, 
but I'd still be interested in a workaround. (GC.collect() to collect 
the remaining Thread objects and thus cause the threads to be detach()ed 
unfortunately breaks on OS X: 
http://d.puremagic.com/issues/show_bug.cgi?id=6135).

> AFAIK, a thread cannot go away until you join it, because it still has
> to give you its exit status.
Not if you call pthread_detach(), after which calls to pthread_join() 
are defined to be invalid.

David


More information about the Digitalmars-d-learn mailing list