http://msdn.microsoft.com/en-us/library/ms686774(VS.85).aspx
MSDN
- MSDN Home
- MSDN Library
- MSDN Learn
- MSDN Downloads
- MSDN Support
- MSDN Community
MSDN Library
- Design Tools
- Development Tools and Languages
- Mobile and Embedded Development
- .NET Development
- Office Development
- Open Specifications
- Servers and Enterprise Development
- Web Development
- Win32 and COM Development
Win32 and COM Development
- Administration and Management
- Component Development
- Data Access and Storage
- Development Guides
- Diagnostics
- Graphics and Multimedia
- Messaging and Collaboration
- Mobile PC
- Networking
- Security
- System Services
- Tools
- User Interface
- Windows Search
- Windows Driver Kit
- Windows Logo Kit
- Windows Versions
- XML
- Technical Articles
System ServicesPlease Wait
DLLs, Processes, and ThreadsPlease Wait
Processes and ThreadsPlease Wait
About Processes and Threads
- Multitasking
- Scheduling
- Multiple Threads
- Child Processes
- Thread Pools
- Job Objects
- Fibers
Multiple Threads
- Thread Stack Size
- Thread Handles and Identifiers
- Suspending Thread Execution
- Synchronizing Execution of Multiple Threads
- Multiple Threads and GDI Objects
- Thread Local Storage
- Creating Windows in Threads
- Terminating a Thread
- Thread Security and Access Rights
Thread Stack Size
Thread Stack Size
Each
new thread or fiber receives its own stack space consisting of both
reserved and initially committed memory. The reserved memory size
represents the total stack allocation in virtual memory. As such, the
reserved size is limited to the virtual address range. The initially
committed pages do not utilize physical memory until they are
referenced; however, they do remove pages from the system total commit
limit, which is the size of the page file plus the size of the physical
memory. The system commits additional pages from the reserved stack
memory as they are needed, until either the stack reaches the reserved
size minus one page (which is used as a guard page to prevent stack
overflow) or the system is so low on memory that the operation fails.
It
is best to choose as small a stack size as possible and commit the
stack that is needed for the thread or fiber to run reliably. Every
page that is reserved for the stack cannot be used for any other
purpose.
A stack is freed when its thread exits. It is not freed if the thread is terminated by another thread.
The
default size for the reserved and initially committed stack memory is
specified in the executable file header. Thread or fiber creation fails
if there is not enough memory to reserve or commit the number of bytes
requested. The default stack reservation size used by the linker is 1
MB. To specify a different default stack reservation size for all
threads and fibers, use the STACKSIZE statement in the module
definition (.def) file. The operating system rounds up the specified
size to the nearest multiple of the system's allocation granularity
(typically 64 KB). To retrieve the allocation granularity of the
current system, use the GetSystemInfo function.
To change the initially committed stack space, use the dwStackSize parameter of the
CreateThread,
CreateRemoteThread, or CreateFiber
function. This value is rounded up to the nearest page. Generally, the
reserve size is the default reserve size specified in the executable
header. However, if the initially committed size specified by dwStackSize
is larger than or equal to the default reserve size, the reserve size
is this new commit size rounded up to the nearest multiple of 1 MB.
To change the reserved stack size, set the dwCreationFlags parameter of
CreateThread or
CreateRemoteThread to STACK_SIZE_PARAM_IS_A_RESERVATION and use the dwStackSize
parameter. In this case, the initially committed size is the default
size specified in the executable header. For fibers, use the dwStackReserveSize parameter of
CreateFiberEx. The committed size is specified in the dwStackCommitSize parameter.
The SetThreadStackGuarantee
function sets the minimum size of the stack associated with the calling
thread or fiber that will be available during any stack overflow
exceptions.
Send comments about this topic to Microsoft
Build date: 8/7/2008