Windows 上的线程堆栈大小 (Visual C++) [英] thread stack size on Windows (Visual C++)

查看:51
本文介绍了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.

推荐答案

虽然没有 API 可以直接找出堆栈大小,但必须保留连续的虚拟地址空间直到最大堆栈大小 - 就这么多那个空间还没有提交.您可以利用这一点并两次调用 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.

对于第一次调用,将堆栈上任何值的地址传递给它,以获取已提交堆栈空间的基地址和大小(以字节为单位).在堆栈向下增长的 x86 机器上,再次从基地址和 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.

这篇关于Windows 上的线程堆栈大小 (Visual C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆