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:28:23 PDT 2011


On 6/10/11 1:21 AM, Robert Clipsham wrote:
> As far as I'm aware, you cannot avoid it, it's a hard coded limit set by
> the operating system. May I suggest using Fibers instead of threads? If
> your threads are short lived, their overhead is probably not worth it.
> You can also combine fibers with threads if you need to take advantage
> of multiple cores (use a couple of worker threads, with N fibers each).

The threads are only short lived during that test, they aren't in the 
real application (incidentally, it would typically be a single thread 
running for the whole application lifetime).

Also, the OS thread limit isn't a problem in my example, as no more than 
a couple of threads are running at the same time – the problem is just 
freeing the handle by doing pthread_detach for the finished threads.

By the way, trying to force a garbage collection as a last resort 
exposes a druntime bug on OS X: 
http://d.puremagic.com/issues/show_bug.cgi?id=6135.

David


More information about the Digitalmars-d-learn mailing list