question about the semantics of unshared variables

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 16 00:43:00 PDT 2015


On Thursday, July 16, 2015 06:53:50 aki via Digitalmars-d-learn wrote:
> I noticed just making many threads cause an error.
> Are there any limit for the number of threads?
>
> import std.concurrency;
> import core.thread;
> void fun() { Thread.sleep(5000.msecs); }
> void testThread() {
>   foreach(i; 0..2000) {
>       spawn(&fun);
>   }
> }
>
> core.thread.ThreadError at src\core\thread.d(2903): Error creating
> thread

The OS has a limit for the number of threads that you can have (though I
would have thought that it was higher than 2000). I'm not aware of any other
limitations, but there might be some. But you may have hit the limit for the
number of threads on your system depending on what OS you're running and
what it's limitations are. Certainly, looking at core.thread, it looks like
it's the C call to create the thread which is failing, which would impy that
the problem is related to the C thread API being used and not what druntime
is doing. You'd have to investigate what the exact error is though.
Unfortunately, druntime doesn't currently get the error code or its
associated message, so I can't tell from what you've posted why the C
function for creating the thread is failing. But aside from debugging it
directly in druntime, you could just create a C/C++ program to create 2000
threads with the C API like you're doing here and see what it does on your
system. That might tell you why your code isn't working.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list