Thread Stack Size

来源:互联网 发布:京都工艺品淘宝 编辑:程序博客网 时间:2024/06/07 15:58
http://msdn.microsoft.com/en-us/library/ms686774(VS.85).aspx
 MSDN
MSDN Library
Win32 and COM Development
System Services
DLLs, Processes, and Threads
Processes and Threads
About Processes and Threads
Multiple Threads
 Thread Stack Size
Thread Stack Size

Eachnew thread or fiber receives its own stack space consisting of bothreserved and initially committed memory. The reserved memory sizerepresents the total stack allocation in virtual memory. As such, thereserved size is limited to the virtual address range. The initiallycommitted pages do not utilize physical memory until they arereferenced; however, they do remove pages from the system total commitlimit, which is the size of the page file plus the size of the physicalmemory. The system commits additional pages from the reserved stackmemory as they are needed, until either the stack reaches the reservedsize minus one page (which is used as a guard page to prevent stackoverflow) or the system is so low on memory that the operation fails.

Itis best to choose as small a stack size as possible and commit thestack that is needed for the thread or fiber to run reliably. Everypage that is reserved for the stack cannot be used for any otherpurpose.

A stack is freed when its thread exits. It is not freed if the thread is terminated by another thread.

Thedefault size for the reserved and initially committed stack memory isspecified in the executable file header. Thread or fiber creation failsif there is not enough memory to reserve or commit the number of bytesrequested. The default stack reservation size used by the linker is 1MB. To specify a different default stack reservation size for allthreads and fibers, use the STACKSIZE statement in the moduledefinition (.def) file. The operating system rounds up the specifiedsize to the nearest multiple of the system's allocation granularity(typically 64 KB). To retrieve the allocation granularity of thecurrent system, use the GetSystemInfo function.

To change the initially committed stack space, use the dwStackSize parameter of the CreateThread, CreateRemoteThread, or CreateFiberfunction. This value is rounded up to the nearest page. Generally, thereserve size is the default reserve size specified in the executableheader. However, if the initially committed size specified by dwStackSizeis larger than or equal to the default reserve size, the reserve sizeis 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 dwStackSizeparameter. In this case, the initially committed size is the defaultsize specified in the executable header. For fibers, use the dwStackReserveSize parameter of CreateFiberEx. The committed size is specified in the dwStackCommitSize parameter.

The SetThreadStackGuaranteefunction sets the minimum size of the stack associated with the callingthread or fiber that will be available during any stack overflowexceptions.


Send comments about this topic to Microsoft

Build date: 8/7/2008

原创粉丝点击