我在类Worker中编写了p_thread:
class Worker {
public:
Worker(int worker_num ,void* (*function)(void*),void * params);
virtual ~Worker();
private:
pthread_t thread;
int worker_num;
};
Worker::Worker(int worker_num, void* (*function)(void*), void * params) {
pthread_create(&thread, NULL, function, (void *) params);
logger.log(DEBUG_LEVEL_DEBUG, "Worker id=%u was created\n", worker_num);
this->worker_num = worker_num;
return;
}
Worker::~Worker() {
pthread_cancel(thread);
logger.log(DEBUG_LEVEL_DEBUG, "Worker id=%u was canceled\n", worker_num);
return;
}
我在另一个类THREAD_POOL的构造函数上创建了一个worker向量:
for (int i = 0; i < worker_num; i++) {
this->workers.push_back(new Worker(i, thread_exe, ((void*) this)));
}
(函数thread_exe是无限循环)。当我试图在THREAD_POOL类的destroctor上销毁那些工人时,如下:
ThreadPool::~ThreadPool() {
for(int worker_index = 0 ; worker_index < workers.size();worker_index++){
delete workers[worker_index];
}
我的程序崩溃了:
工人ID = 0被取消 检测到 * glibc * /.autodirect/fwgwork/yehudas/workspace/dt_utils/code_coverage/Debug/fcollect: double free或corruption(out):0x0000000000629590 * ======= Backtrace:========= / lib64 / libc.so.6 [0x3300476166] /lib64/libc.so.6[0x3300478c93] /.autodirect/fwgwork/yehudas/workspace/dt_utils/code_coverage/Debug/fcollect[0x413ae2] /.autodirect/fwgwork/yehudas/workspace/dt_utils/code_coverage/Debug/fcollect[0x412a04] /.autodirect/fwgwork/yehudas/workspace/dt_utils/code_coverage/Debug/fcollect[0x412b70] /.autodirect/fwgwork/yehudas/workspace/dt_utils/code_coverage/Debug/fcollect[0x40e527] /.autodirect/fwgwork/yehudas/workspace/dt_utils/code_coverage/Debug/fcollect[0x4083fe] /lib64/libc.so.6(__libc_start_main+0xfd)[0x330041ed1d] /.autodirect/fwgwork/yehudas/workspace/dt_utils/code_coverage/Debug/fcollect[0x403a69] =======记忆图:======== 00400000-00426000 r-xp 00000000 00:21 19570593
/.autodirect/fwgwork/yehudas/workspace/dt_utils/code_coverage/Debug/fcollect 00625000-00626000 rw-p 00025000 00:21 19570593
/.autodirect/fwgwork/yehudas/workspace/dt_utils/code_coverage/Debug/fcollect 00626000-00648000 rw-p 00000000 00:00 0
[堆] 32ffc00000-32ffc20000 r-xp 00000000 fc:01 670751
/lib64/ld-2.12.so 32ffe1f000-32ffe20000 r - p 0001f000 fc:01 670751
/lib64/ld-2.12.so 32ffe20000-32ffe21000 rw-p 00020000 fc:01 670751
/lib64/ld-2.12.so 32ffe21000-32ffe22000 rw-p 00000000 00:00 0 3300000000-3300083000 r-xp 00000000 fc:01 670759
/lib64/libm-2.12.so 3300083000-3300282000 --- p 00083000 fc:01 670759
/lib64/libm-2.12.so 3300282000-3300283000 r - p 00082000 fc:01 670759
/lib64/libm-2.12.so 3300283000-3300284000 rw-p 00083000 fc:01 670759
/lib64/libm-2.12.so 3300400000-330058b000 r-xp 00000000 fc:01 670752
/lib64/libc-2.12.so 330058b000-330078a000 --- p 0018b000 fc:01 670752
/lib64/libc-2.12.so 330078a000-330078e000 r - p 0018a000 fc:01 670752
/lib64/libc-2.12.so 330078e000-330078f000 rw-p 0018e000 fc:01 670752
/lib64/libc-2.12.so 330078f000-3300794000 rw-p 00000000 00:00 0 3300800000-3300802000 r-xp 00000000 fc:01 670755
/lib64/libdl-2.12.so 3300802000-3300a02000 --- p 00002000 fc:01 670755 /lib64/libdl-2.12.so 3300a02000-3300a03000 r - p 00002000 fc:01 670755 /lib64/libdl-2.12.so 3300a03000-3300a04000 rw-p 00003000 fc:01 670755 /lib64/libdl-2.12.so 3300c00000-3300c17000 r-xp 00000000 fc:01 670753 /lib64/libpthread-2.12.so 3300c17000-3300e17000 --- p 00017000 fc:01 670753 /lib64/libpthread-2.12.so 3300e17000-3300e18000 r - p 00017000 fc:01 670753
/lib64/libpthread-2.12.so 3300e18000-3300e19000 rw-p 00018000 fc:01 670753 /lib64/libpthread-2.12.so 3300e19000-3300e1d000 rw-p 00000000 00:00 0 3303c00000-3303c16000 r-xp 00000000 fc:01 670760
/lib64/libgcc_s-4.4.7-20120601.so.1 3303c16000-3303e15000 --- p 00016000 fc:01 670760
/lib64/libgcc_s-4.4.7-20120601.so.1 3303e15000-3303e16000 rw-p 00015000 fc:01 670760
/lib64/libgcc_s-4.4.7-20120601.so.1 3304800000-33048e8000 r-xp 00000000 fc:01 918134
/usr/lib64/libstdc++.so.6.0.13 33048e8000-3304ae8000 --- p 000e8000 fc:01 918134 /usr/lib64/libstdc++.so.6.0.13 3304ae8000-3304aef000 r - p 000e8000 fc:01 918134
/usr/lib64/libstdc++.so.6.0.13 3304aef000-3304af1000 rw-p 000ef000 fc:01 918134 /usr/lib64/libstdc++.so.6.0.13 3304af1000-3304b06000 rw-p 00000000 00:00 0 7fffe8000000-7fffe8021000 rw-p 00000000 00:00 0 7fffe8021000-7fffec000000 --- p 00000000 00:00 0 7ffff0000000-7ffff0021000 rw-p 00000000 00:00 0 7ffff0021000-7ffff4000000 --- p 00000000 00:00 0 7ffff6bdb000-7ffff6bdc000 --- p 00000000 00:00 0 7ffff6bdc000-7ffff75dc000 rw-p 00000000 00:00 0 7ffff75dc000-7ffff75dd000 --- p 00000000 00:00 0 7ffff75dd000-7ffff7fe3000 rw-p 00000000 00:00 0 7ffff7ffa000-7ffff7ffe000 rw-p 00000000 00:00 0 7ffff7ffe000-7ffff7fff000 r-xp 00000000 00:00 0
[vdso] 7ffffffe9000-7ffffffff000 rw-p 00000000 00:00 0
[stack] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]
需要建议...
答案 0 :(得分:1)
你忘了增加循环计数器(++ worker_index)
ThreadPool::~ThreadPool() {
for(int worker_index = 0 ; worker_index < workers.size(); ++worker_index){
delete workers[worker_index];
}