Quote:
Originally Posted by wollowizard
of course I reach the maximum number.
|
Note that you can get a lot more threads if you reduce the default thread stack size.
Only the original stack given to a process grows automatically. Threads are limited to a fixed-size stack. The default size is
ulimit -s (in kilobytes), default 8192 or 8 megabytes. On a 32-bit system that limits the number of threads a process can have to about 350 or so (assuming 3G/1G split; 240 or so if using 2G/2G memory split).
It is simple to use a different stack size, by supplying suitable pthread attribute to
pthread_create():
Code:
pthread_attr_t attrs;
pthread_attr_init(&attrs);
pthread_attr_setstacksize(&attrs, 65536);
/* After no longer needed, */
pthread_attr_destroy(&attrs);
The attribute is not "consumed" by a
pthread_create() call; it is just a set of configuration defaults
pthread_create() looks at when it creates a new thread. You can create any number of threads using the same attribute. You can destroy the attribute immediately after creating the threads (while the threads are still running).
The stack size is specified in bytes, and is typically 16384 bytes minimum (
PTHREAD_STACK_MIN). I recommend using a power of two, or a multiple of
sysconf(_SC_PAGE_SIZE) (otherwise the call is allowed to fail).
The amount of memory your threads need depend on how large the local variables are in its call chain.
In practice, I just look at the deepest case, sum the sizes of all the local variables in the function and its callers, add the 16k (minimum), plus a few k for overhead (like signal handlers and so on). There are tools you can use to find out the stack usage in real world usage, but I've found this to yield better results. If your call chains are complex, then don't allocate arrays or large structures on stack, and avoid recursive functions, and you shouldn't need much larger stack than the minimum.