Maximum number of threads

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 24 04:40:19 PDT 2015


On Thursday, September 24, 2015 08:55:22 Alex via Digitalmars-d-learn wrote:
> This should be a not so long question to answer, I hope.
>
> I took an example from the "Programming in D" book, chapter
> "Message Passing Concurrency", around page 550. The question of
> interest was, how many threads I can have spawned at the same
> time.
> So I made an array of robot objects from the example and found
> out: the maximum number is different on different PCs I have.
> For example:
> - on my mac I can have 2048 threads spawned at the same time
> - on my linux machine the maximum number is 32192.
> The numbers are quite fixed, however there were some small
> fluctuations on the linux machine.
>
> The questions still remains: how do I know, what maximum number
> of threads I can have? Is it possible to calculate this value
> during runtime of my program? The two machines I have available
> for testing are very different, what is the main parameter which
> controls the number of possible threads? (CPU maybe? Number of
> cores? RAM is not I think at the moment...)

It's entirely system specific. Not only does every OS handle it differently,
but it con depend on how your machine is configured. In general though, I
believe that it's a hard limit in the OS and has nothing to do with the
number of cores or amount of RAM in your machine (though even if you haven't
hit the limit, if the OS doesn't have enough resources to create another
thread, then it will fail).

So, if you want to know for a particular OS, you're probably going to have
to google for it. In the case of linux, a quick search seems to indicate
that getrlimit will do the trick:

http://linux.die.net/man/2/getrlimit

But you can find as much pretty quickly googling yourself. Maybe someone
else can tell you what to use on other OSes off the top of their head
though.

Regardless, D doesn't provid any special way to do this, so to figure it
out, you'd be calling whatever the C functions are that will tell you.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list