Windows 上的线程堆栈大小 (Visual C++)
是否有调用来确定正在运行的线程的堆栈大小?我一直在查找 MSDN 线程函数文档,但似乎找不到。
Is there a call to determine the stack size of a running thread? I've been looking in MSDN thread functions documentation, and can't seem to find one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然没有 API 可以直接找出堆栈大小,但必须保留最大堆栈大小的连续虚拟地址空间 - 只是很多空间尚未提交。您可以利用这一点并对 VirtualQuery< 进行两次调用/a>.
对于第一次调用,将堆栈上任何值的地址传递给它,以获取已提交堆栈空间的基地址和大小(以字节为单位)。在堆栈向下增长的 x86 机器上,再次从基地址和 VirtualQuery 中减去大小:这将为您提供为堆栈保留的空间大小(假设您当时没有完全达到堆栈大小的限制) )。将两者相加自然可以得出总堆栈大小。
Whilst there isn't an API to find out stack size directly, contiguous virtual address space must be reserved up to the maximum stack size - it's just that a lot of that space isn't committed yet. You can take advantage of this and make two calls to VirtualQuery.
For the first call, pass it the address of any value on the stack to get the base address and size, in bytes, of the committed stack space. On an x86 machine where the stack grows downwards, subtract the size from the base address and VirtualQuery again: this will give you the size of the space reserved for the stack (assuming you're not precisely on the limit of stack size at the time). Summing the two naturally gives you the total stack size.
您可以从 TEB 中的顶部和底部获取当前提交的大小。您可以从 PE 标头获取进程初始保留和提交大小。但您无法检索传递给 CreateThread,也没有任何 API 可以获取当前堆栈中保留或提交的剩余大小,请参阅 线程堆栈大小。
You can get the current committed size from the Top and Bottom in the TEB. You can get the process initial reserve and commit sizes from the PE header. But you cannot retrieve the actual sizes passed to CreateThread, nor is there any API to get the remaining size of reserved nor committed from current stack, see Thread Stack Size.