pet*_*ohn 10 c++ multithreading valgrind g++ boost-thread
我有一个程序在一个单独的therad中运行一些动作,然后加入线程,比如这个:
#include <boost/thread.hpp>
#include <iostream>
using namespace std;
void f() {
for (int i = 0; i < 100; ++i) cout << i << endl;
}
int main() {
boost::thread t(f);
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我在其上运行Valgrind,它会报告"可能丢失"的内存.如果省略join(),这似乎是合乎逻辑的,因为在这种情况下,当程序退出时线程仍在运行.但如果线程完成,我希望没有警告.
这是回溯:
==8797== 288 bytes in 1 blocks are possibly lost in loss record 2 of 3
==8797== at 0x4A1F8B3: calloc (vg_replace_malloc.c:467)
==8797== by 0x400F289: allocate_dtv (in /lib64/ld-2.4.so)
==8797== by 0x400F34D: _dl_allocate_tls (in /lib64/ld-2.4.so)
==8797== by 0x53EF981: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.4.so)
==8797== by 0x4B3311D: boost::thread::start_thread() (in /home/egbomrt/BOOST/inst_1_47_0/lib/libboost_thread.so.1.47.0)
==8797== by 0x40A20C: boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type) (thread.hpp:204)
==8797== by 0x406295: main (main.cpp:12)
Run Code Online (Sandbox Code Playgroud)
这是Boost Thread,Posix Thread的问题还是这个完全正常?我可以为它创建一个抑制规则,但如果我有一个未完成的线程,但是当所有线程都完成时,我会收到警告.
我发现问题出在 pthread 库上。如果我在 SUSE 10 上运行该程序,则会出现内存泄漏,但如果在 SUSE 11 上运行该程序,则不会出现该问题。
使用和不使用 Boost 时得到的结果相同。
感谢您的评论。这帮助我查明了问题。
| 归档时间: |
|
| 查看次数: |
932 次 |
| 最近记录: |