0
我有下面的代碼,當在valgrind下運行時,仍然表示某些塊可以訪問。雖然代碼沒有任何明確的泄漏。 這是爲什麼發生。仍然可以在valgrind輸出塊中輸出std線程的矢量
請幫忙?
的valgrind跡是
==5059== 32 bytes in 1 blocks are still reachable in loss record 1 of 1
==5059== at 0x4C2C20C: operator new(unsigned long) (vg_replace_malloc.c:334)
==5059== by 0x402A67: __gnu_cxx::new_allocator<std::thread>::allocate(unsigned long, void const*) (new_allocator.h:104)
==5059== by 0x402986: std::allocator_traits<std::allocator<std::thread> >::allocate(std::allocator<std::thread>&, unsigned long) (alloc_traits.h:416)
==5059== by 0x40280F: std::_Vector_base<std::thread, std::allocator<std::thread> >::_M_allocate(unsigned long) (stl_vector.h:170)
==5059== by 0x402493: void std::vector<std::thread, std::allocator<std::thread> >::_M_emplace_back_aux<std::thread>(std::thread&&) (vector.tcc:412)
==5059== by 0x402008: void std::vector<std::thread, std::allocator<std::thread> >::emplace_back<std::thread>(std::thread&&) (vector.tcc:101)
==5059== by 0x40188F: std::vector<std::thread, std::allocator<std::thread> >::push_back(std::thread&&) (stl_vector.h:933)
==5059== by 0x4012D0: main (t3.cpp:25)
==5059== LEAK SUMMARY:
==5059== definitely lost: 0 bytes in 0 blocks
==5059== indirectly lost: 0 bytes in 0 blocks
==5059== possibly lost: 0 bytes in 0 blocks
==5059== still reachable: 32 bytes in 1 blocks
==5059== suppressed: 0 bytes in 0 blocks
==5059==
的代碼如下。
#include<iostream>
#include<vector>
#include<string>
#include<mutex>
#include<thread>
using namespace std;
std::mutex g_mutex;
void dosomework(const int& id)
{
std::lock_guard<std::mutex> lock(g_mutex);
cout << "I am doing some work in thread id = " << id << endl;
}
int main(int argc, char* argv[])
{
std::vector<std::thread> threads;
threads.reserve(3);
for(unsigned int i=0; i<3; ++i)
threads.push_back(std::thread(dosomework,i));
std::this_thread::sleep_for(std::chrono::seconds(10));
for(auto& t : threads)
{
if(t.joinable())
{
cout << "joining the thread" << endl;
t.join();
}
else
{
cout << "thread is not joinable" << endl;
}
}
exit(0);
}
是的基本知識!我錯過了那一個。不好的想法使用退出,應該幾乎總是避免它。 –