Thread overview
question about the semantics of unshared variables
Jul 15, 2015
aki
Jul 16, 2015
aki
Jul 16, 2015
Jonathan M Davis
Jul 16, 2015
QAston
Jul 16, 2015
Jonathan M Davis
Jul 16, 2015
aki
July 15, 2015
I want to make sure about the semantics of unshared variables.

import std.concurrency;
import core.thread;
ubyte[1024 * 1024] buf1MB;
void fun() { Thread.sleep(5000.msecs); }
void testThread() {
	foreach(i; 0..2000) {
		spawn(&fun);
	}
}

Are instances of buf1MB created for every threads?
Even if it is never accessed by the threads?
If some static variable is defined in other module
or in some library, all of them also instantiated
for every threads?
BTW, calling testThread() above causes following
run time error. Is this an expected result?

core.thread.ThreadError@src\core\thread.d(2903): Error creating thread
----------------
0x0047B1A7
0x0042ABD4
0x0042AB3B
0x0042AB27
0x0042ADF9
0x00474C22
0x00474BF7
0x00474B0F
0x0042AE13
0x757A7C04 in BaseThreadInitThunk
0x7735AD1F in RtlInitializeExceptionChain
0x7735ACEA in RtlInitializeExceptionChain

July 16, 2015
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@src\core\thread.d(2903): Error creating thread

Aki.

July 16, 2015
On Wednesday, July 15, 2015 16:21:29 aki via Digitalmars-d-learn wrote:
> I want to make sure about the semantics of unshared variables.
>
> import std.concurrency;
> import core.thread;
> ubyte[1024 * 1024] buf1MB;
> void fun() { Thread.sleep(5000.msecs); }
> void testThread() {
>   foreach(i; 0..2000) {
>       spawn(&fun);
>   }
> }
>
> Are instances of buf1MB created for every threads?
> Even if it is never accessed by the threads?
> If some static variable is defined in other module
> or in some library, all of them also instantiated
> for every threads?

Yes. Every thread gets a copy of the non-shared static variables, and all of the non-shared static constructors get run for each thread.

- Jonathan M Davis

July 16, 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@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

July 16, 2015
On Thursday, 16 July 2015 at 07:36:39 UTC, Jonathan M Davis wrote:
> Yes. Every thread gets a copy of the non-shared static variables, and all of the non-shared static constructors get run for each thread.
>
> - Jonathan M Davis

Thank you for reply. Now i know.
I did some test using C++ as you said, then there are some
different problem occurred. Anyway no need to make so
many threads. Please just forget it.

Aki.

July 16, 2015
On Thursday, 16 July 2015 at 07:43:10 UTC, Jonathan M Davis wrote:
> [...]

On linux you can alter the limit by using ulimit command. -a option shows the current limits.