c++ - 确定 posix pthread 的堆栈使用情况?

标签 c++ pthreads stack

如何确定多线程 C++ 程序中当前线程使用的当前堆栈空间量?我正在使用 POSIX pthreads。

我有一个多线程程序,其中一个堆栈爆炸了,但我不知道是哪一个。所以我有兴趣让每个线程向日志文件报告当前堆栈使用情况,这应该是该线程的堆栈开始和当前堆栈指针之间的差异。我知道如何用汇编语言执行此操作,但我正在寻找一种使用 pthreads 调用或类似方法的可移植方法。

谢谢。

最佳答案

I have a multi-threaded program where one of the stacks is blowing up, but I can't figure out which one.

让程序崩溃,留下一个core文件。将该核心加载到调试器中,它会立即告诉您哪个线程崩溃了。

The thread that crashed is not necessarily the one with the overflow.

确实存在一个线程溢出堆栈,但另一个线程因此崩溃的情况。

但是,这种情况极其很少见(至少对于非用户分配的线程堆栈——pthread 库通常会提供一个堆栈保护页面来防止这种情况)。我只能想到一种这样的情况,在实践中这种情况不太可能发生。

I'm looking for a portable approach using a pthreads call or something similar.

没有可移植的方式来实现您想要的。在 Linux 上,您可以使用 pthread_getattr_nppthread_attr_getstack 查找当前线程堆栈的边界,并从那里轻松计算当前堆栈使用情况。

关于c++ - 确定 posix pthread 的堆栈使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18265523/

相关文章:

c++ - 函数结束后值发生变化

php - 在 Ubuntu 上启用 ZTS 重新编译 PHP

python - ZeroVM导入错误: No module named thread

c# - Stack 中元素的弹出范围

android - Qt OpenCV Android 代码不会在 capture.grab() 之前移动

c++ - OpenCL:输出可变长度的数组

c - 在C中的thread_join之后重用线程结构

linux-kernel - Linux内核ARM异常栈init

c++ - Qt:如何在使用 customContextMenuRequested 信号时检测右键单击的项目

c - 我很难让一个结构使用多个文件。请帮忙?