What does _beginthread's second argument stack_size mean?
In function _beginthread, what does the second argument (stack_size) mean?
S开发者_如何学JAVAtack size of where? And what does the default value (0) mean?
The stack size of where?
The call stack is a stack that maintains information about active function calls of executing software. It's also known as an execution stack, control stack, or run-time stack. In multi-threaded software, each thread has its own call stack.
The primary purpose of the call stack is to manage control flow by keeping track of where each function call returns to. When a function call is made, a new stack frame is pushed onto the stack for that function. When the function returns, its stack frame is popped off and control flow is returned to the address of the caller's next instruction.
A stack frame typically includes:
- Return address back to caller
- Parameters passed to the function
- Saved registers & local variables
Parameters can also be passed via CPU registers, but there are drawbacks to this (ie. limited number of parameters, & registers may be needed for computation.)
Similarly, all local variables don't have to be allocated on the current stack frame. Languages that support closures require free variables to survive after function return, yet locals on the call stack are deallocated when the current stack frame is popped off and control is returned to the caller.
My point here is that parameter passing and allocation of locals are determined by language and compiler implementation; you shouldn't assume they always exist on the stack.
What does stack_size mean? What is the default value 0?
From the MSDN documentation on _beginthread, found under the Remarks section:
The operating system handles the allocation of the stack when either _beginthread or _beginthreadex is called; you do not need to pass the address of the thread stack to either of these functions.
In addition, the stack_size argument can be 0, in which case the operating system uses the same value as the stack specified for the main thread.
精彩评论