David
David

Reputation: 1265

Total stack sizes of threads in one process

I use pthreads_attr_getthreadsizes() to get default stack size of one thread, 8MB on my machine.

But when I create 8 threads and allocate a very large stack size to them, say hundreds of MB, the program crash.

So, I guess, shall

("Number of threads" * "stack size per thread") < a constant value (e.g. virtual memory size)

?

Upvotes: 0

Views: 1555

Answers (1)

caf
caf

Reputation: 239041

The short answer is "Yes".

The longer answer is that all of your threads share one virtual address space, and userspace-usable part of this space must be therefore be large enough to contain all thread stacks (as well as the code, static data, heap, libraries and any miscellaneous mappings).

Multi-hundred-megabyte stacks are a good indication that You're Doing It Wrong, as they say in the classics.

Upvotes: 1

Related Questions