繁体   English   中英

C++ 线程的可用堆栈大小

[英]Available stack size for C++ thread

我使用 Windows 10、Visual Studio 2019 程序生成线程。 我需要添加功能,指出在任何执行时间内可用的堆栈大小。

#include <iostream>
#include <thread>

void thread_function()
{
    //AvailableStackSize()?  
    //Code1 //varibales on stack allocation + function call
    //AvailableStackSize()? it should decrease
    //Code2 //varibales on stack allocation + function call
    //AvailableStackSize()? it should decrease
    //Code3 //varibales on stack allocation + function call
    //AvailableStackSize()? it should decrease
}

int main()
{
    std::thread t(&thread;_function);
    std::cout << "main thread\n";
    std::thread t2 = t;

    t2.join();

    return 0;
}

我尝试使用。 但我不确定我该如何继续。 我只能查询堆栈的总大小。 不是一个可用的。

bool AvailableStackSize()
{
    // Get the stack pointer
    PBYTE pEsp;
    _asm {
        mov pEsp, esp
    };

    // Query the accessible stack region
    MEMORY_BASIC_INFORMATION mbi;
    VERIFY(VirtualQuery(pEsp, &mbi, sizeof(mbi)));

    return mbi.RegionSize;//This is a total size! what is an available? Where guard is located?

}

可能我还可以检查堆栈内是否有一些地址

PVOID add;
(add>= mbi.BaseAddress) && (add < PBYTE(mbi.BaseAddress) + mbi.RegionSize);

我看到了几个类似的问题,但没有人 100% 回答这个问题。 获得可用堆栈大小的正确方法是什么?

由于堆栈向下增长,它是堆栈的潜在底部和当前使用的末尾之间的差异。 顺便说一句,您不需要任何内联汇编,因为您知道本地人放在堆栈上。

__declspec(noinline) size_t AvailableStackSize()
{
    // Query the accessible stack region
    MEMORY_BASIC_INFORMATION mbi;
    VERIFY(VirtualQuery(&mbi, &mbi, sizeof(mbi)));

    return uintptr_t(&mbi) - uintptr_t(mbi.AllocationBase);
}

这将与调用者中实际可用的内容略有不同,因为 function 调用使用了一些(存储返回地址和任何保留的寄存器)。 您需要调查最终保护页面是出现在相同的VirtualQuery结果还是相邻的结果中。

但这是一般方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM