c++ - 为什么 Valgrind 使用 boost::thread 显示堆栈使用量增加?

标签 c++ linux valgrind boost-thread

写了一个简单的测试:

#include <iostream>
#include <boost/thread.hpp>

using namespace std;

void myThreadRun() {
    cout << "Thread id: " << boost::this_thread::get_id() << "\n";
}

int main() {
    for (int i = 0; i < 10000; i++) {
        boost::thread t(myThreadRun);

        t.join();
    }

    return 0;
}

Valgrind Massif 在其上显示以下图表:

Valgrind Massif profiling result for the above example

(堆栈分析已启用。平台:Linux Ubuntu x86)。

这个程序实际上似乎没有内存泄漏:内存使用是稳定的。

我想知道:这是 Valgrind 还是 boost::thread 的问题?还是我误解了什么?

你会怎么解释?

最佳答案

这不是 boost::threads,它也发生在普通的 pthreads 上。我从 here 获取了示例程序(Pthread 创建和终止),将线程数增加到 1000 并编译为普通 C,我在使用地 block 处理它时看到了相同的行为。所以要么是 pthreads 中的东西,要么是 valgrind/massif 正在做的事情。

编辑:也使用过程序(Pthread Joining)。见第二张图。

创建和终止:

    KB
547.6^                                                                       #
     |                                                                    @@@#
     |                                                                @@@@@@@#
     |                                                             @@@@@@@@@@#
     |                                                          @@@@@@@@@@@@@#
     |                                                      ::::@@@@@@@@@@@@@#
     |                                                  ::::: ::@@@@@@@@@@@@@#
     |                                               @@@::::: ::@@@@@@@@@@@@@#
     |                                           @@@@@@@::::: ::@@@@@@@@@@@@@#
     |                                        @@@@@@@@@@::::: ::@@@@@@@@@@@@@#
     |                                     @@@@@@@@@@@@@::::: ::@@@@@@@@@@@@@#
     |                                @@@@@@@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@#
     |                            @@@@@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@#
     |                         @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@#
     |                     ::@@@@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@#
     |                  @@@::@ @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@#
     |               @@@@ @::@ @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@#
     |           @@@@@@ @ @::@ @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@#
     |      :::::@@@ @@ @ @::@ @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@#
     |   :@@: :: @@@ @@ @ @::@ @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@#
   0 +----------------------------------------------------------------------->Mi
     0                                                                   13.22

Pthread 加入,减去数学繁忙的工作:

    KB
548.8^                                                             #
     |                                                           @@#::
     |                                                        :::@@#::
     |                                                     ::::::@@#:::
     |                                                  ::::: :::@@#:::::
     |                                              @@@@::::: :::@@#:::::
     |                                            @@@@@ ::::: :::@@#:::::::
     |                                        :@@:@@@@@ ::::: :::@@#:::::::@
     |                                     @@@:@ :@@@@@ ::::: :::@@#:::::::@
     |                                  :::@ @:@ :@@@@@ ::::: :::@@#:::::::@::
     |                               @@@:: @ @:@ :@@@@@ ::::: :::@@#:::::::@::
     |                            @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@::
     |                        @:::@@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@::
     |                     ::@@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@::
     |                  ::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@::
     |               :@@::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@::
     |            @@@:@ ::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@::
     |         @@@@@ :@ ::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@::
     |     @@@:@@@@@ :@ ::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@::
     |   @@@@ :@@@@@ :@ ::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@::
   0 +----------------------------------------------------------------------->Mi
     0                                                                   19.14

看起来加入最终应该会降低堆栈大小,即使在 valgrind 下也是如此。

关于c++ - 为什么 Valgrind 使用 boost::thread 显示堆栈使用量增加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12427016/

相关文章:

c - 我不明白为什么我会收到这个 valgrind 错误

c++ - 优化器优化 C++ 或程序集的作用是什么

linux - 尝试在 Ubuntu 1804 上安装 PCAN 驱动程序时出现 Modprobe 错误

c - 让: ** NO targets specififed and no makefile found.停止

linux - 没有gui linux的rdesktop请求

android - 总是恢复到旧版本

c - 当我使用 valgrind 运行这段代码时,它的执行方式与我在没有 valgrind 的情况下运行它时的执行方式不同

c++ - 在 C++ 中观察汇编器中虚函数的后期绑定(bind)调用

c++ - 取整并通过 XML-RPC 发送

c++ - C++ 奇怪行为中的 Lambda 函数