Allocating and Managing a Thread Stack
The responsibility for allocating and managing a thread stack is usually left to the system, but some environments allow users to create and manage their own stack space.
If the system is given the responsibility for allocating and managing the stack, then you might also have the opportunity to choose whether to commit physical memory and pagefile space to the stack at the time of its creation or to commit memory on-demand as the stack grows.
To support each of these variations, the Threading package defines several attributes that can be used to select and define the desired stack allocation policy:
 
Thread Stack Attributes
Stack Allocation Policy
Stack Reserve Size
Stack Commit Size
User Stack Address and Size
System Allocated
Commit On Demand
X
 
 
Commit At Creation
 
X
 
Mixed
X
X
 
User Allocated
 
 
X
Issues to consider when using stack attributes:
*Unless the user stack attributes are set, the RWThreadAttribute class always assumes that the underlying system has responsibility for allocating and managing the stack.
*Use of the stack attributes is optional. You can safely ignore these attributes in most situations because they usually assume reasonable default values.
*Consider manipulating these attributes if you have threads that make very large stack allocations, perhaps as the result of automatic array allocations.
*If you decide that you want to define the stack size, either for a system-managed or user-managed stack, remember to take into account the additional stack space that might required by code executing in other libraries.
*Some run-time environments place demands on a thread’s stack, and some dynamic linker-loaders require use of a thread’s stack when locating and attaching to library code or data.
*Stack overruns in a multithreaded environment can be quite difficult to detect and debug, so you should use care in limiting your stack sizes. You are generally better off if you can make your stacks bigger instead of smaller.
*Some of the stack attributes mentioned here are mutually exclusive. If you set the stack reserve or commit size, any previous definitions for a user-managed stack are discarded. If the underlying environment does not support partial stack commitment, the Threading package also imposes mutual exclusion between the reserve size and commit size attributes.