65,209
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <pthread.h>
#include <vector>
using namespace std;
void* thread_func(void* args)
{
int * q = new int;
return 0;
}
int main(int argc,char *argv[])
{
if(argc!=2)
{
cout<<"Run with:"<<argv[0]<<" thread_count"<<endl;
return -1;
}
unsigned int NUM_THREADS= atoi(argv[1]);
pthread_t tids[NUM_THREADS];
for(int i = 0; i < NUM_THREADS; ++i)
{
int ret = pthread_create(&tids[i], NULL, thread_func, NULL);
if (ret != 0)
{
sleep(2);
cout << "pthread_create error: error_code=" << ret << endl;
}
}
pthread_exit(NULL);
}
==================
==26418==
==26418== HEAP SUMMARY:
==26418== in use at exit: 2,920 bytes in 20 blocks
==26418== total heap usage: 25 allocs, 5 frees, 5,424 bytes allocated
==26418==
==26418== 40 bytes in 10 blocks are definitely lost in loss record 1 of 2
==26418== at 0x4C2877F: operator new(unsigned long) (vg_replace_malloc.c:298)
==26418== by 0x400C31: thread_func(void*) (in /leak)
==26418== by 0x4E35850: start_thread (in /lib64/libpthread-2.12.so)
==26418== by 0x58D394C: clone (in /lib64/libc-2.12.so)
==26418==
==26418== 2,880 bytes in 10 blocks are possibly lost in loss record 2 of 2
==26418== at 0x4C266D5: calloc (vg_replace_malloc.c:593)
==26418== by 0x4011892: _dl_allocate_tls (in /lib64/ld-2.12.so)
==26418== by 0x4E35068: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.12.so)
==26418== by 0x400BA2: main (in /leak)
==26418== by 0x5809CDC: __libc_start_main (in /lib64/libc-2.12.so)
==26418== by 0x400998: ??? (in /leak)
==26418== by 0x7FF000737: ???
==26418== by 0x1B: ???
==26418== by 0x1: ???
==26418== by 0x7FF0009DA: ???
==26418== by 0x7FF0009E7: ???
==26418==
==26418== LEAK SUMMARY:
==26418== definitely lost: 40 bytes in 10 blocks
==26418== indirectly lost: 0 bytes in 0 blocks
==26418== possibly lost: 2,880 bytes in 10 blocks
==26418== still reachable: 0 bytes in 0 blocks
==26418== suppressed: 0 bytes in 0 blocks
==26418==
==26418== For counts of detected and suppressed errors, rerun with: -v
==26418== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)#include <iostream>
#include <pthread.h>
#include <vector>
using namespace std;
void* thread_func(void* args)
{
pthread_exit(NULL);
}
int main(int argc,char *argv[])
{
if(argc!=2)
{
cout<<"Run with:"<<argv[0]<<" thread_count"<<endl;
return -1;
}
unsigned int NUM_THREADS= atoi(argv[1]);
pthread_t tids[NUM_THREADS];
for(int i = 0; i < NUM_THREADS; ++i)
{
int ret = pthread_create(&tids[i], NULL, thread_func, NULL);
if (ret != 0)
{
sleep(2);
cout << "pthread_create error: error_code=" << ret << endl;
}
}
return 0;
}
#include <iostream>
#include <pthread.h>
#include <vector>
using namespace std;
void* thread_func(void* args)
{
return 0;
}
int main(int argc,char *argv[])
{
if(argc!=2)
{
cout<<"Run with:"<<argv[0]<<" thread_count"<<endl;
return -1;
}
unsigned int NUM_THREADS= atoi(argv[1]);
pthread_t tids[NUM_THREADS];
for(int i = 0; i < NUM_THREADS; ++i)
{
int ret = pthread_create(&tids[i], NULL, thread_func, NULL);
if (ret != 0)
{
sleep(2);
cout << "pthread_create error: error_code=" << ret << endl;
}
}
pthread_exit(NULL);
}
==12919== Memcheck, a memory error detector
==12919== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==12919== Using Valgrind-3.8.0 and LibVEX; rerun with -h for copyright info
==12919== Command: /etc/SU/leak 10
==12919== Parent PID: 20231
==12919==
==12919==
==12919== HEAP SUMMARY:
==12919== in use at exit: 2,880 bytes in 10 blocks
==12919== total heap usage: 15 allocs, 5 frees, 5,384 bytes allocated
==12919==
==12919== 2,880 bytes in 10 blocks are possibly lost in loss record 1 of 1
==12919== at 0x4C266D5: calloc (vg_replace_malloc.c:593)
==12919== by 0x4011892: _dl_allocate_tls (in /lib64/ld-2.12.so)
==12919== by 0x4E35068: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.12.so)
==12919== by 0x400B62: main (in /PLATsoftware/cs/sn/SU/leak)
==12919== by 0x5809CDC: __libc_start_main (in /lib64/libc-2.12.so)
==12919== by 0x400948: ??? (in /PLATsoftware/cs/sn/SU/leak)
==12919== by 0x7FF000737: ???
==12919== by 0x1B: ???
==12919== by 0x1: ???
==12919== by 0x7FF0009DA: ???
==12919== by 0x7FF0009E7: ???
==12919==
==12919== LEAK SUMMARY:
==12919== definitely lost: 0 bytes in 0 blocks
==12919== indirectly lost: 0 bytes in 0 blocks
==12919== possibly lost: 2,880 bytes in 10 blocks
==12919== still reachable: 0 bytes in 0 blocks
==12919== suppressed: 0 bytes in 0 blocks
==12919==
==12919== For counts of detected and suppressed errors, rerun with: -v
==12919== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
现在应该是清晰的了吧?大神们,请回答?