在测试函数中调用StopAll线程退出失败,产生SIGSEGV信号,原因是什么?
#include "threadpool.h"
#include <iostream>
#include <vector>
using namespace std;
CTask::CTask(string name)
{
this->m_strTaskName = name;
this->m_ptrData = NULL;
}
void CTask::setdata(void *data)
{
this->m_ptrData = data;
}
/********************************************************************************************************************************************/
/**
@static 变量在类外进行初始化
*/
vector<CTask *> CThreadPool::m_vecTaskList;
bool CThreadPool::shutdown = false;
pthread_mutex_t CThreadPool::m_pthreadMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t CThreadPool::m_pthreadCond = PTHREAD_COND_INITIALIZER;
/**
@ 构造函数初始化要创建线程池里的线程数量
*/
CThreadPool::CThreadPool(int threadNum)
{
this->m_iThreadNum = threadNum;
this->CreatePool();
}
/**
@ 向任务队列中添加任务,需要进行加锁处理
@ 成功返回0
*/
int CThreadPool::AddTask(CTask *task)
{
pthread_mutex_lock(&m_pthreadMutex);
this->m_vecTaskList.push_back(task);
cout << "add task successful the task vector size is" << this->getTaskSize() << endl;
pthread_mutex_unlock(&m_pthreadMutex);
pthread_cond_signal(&m_pthreadCond);
return 0;
}
/**
@ 唤醒所有线程让其退出并且销毁互斥锁与条件变量释放内存空间
@ 重复关闭返回-1
@ 成功关闭返回0
*/
int CThreadPool::StopAll()
{
if(shutdown)
{
return -1;
}
shutdown = true;
pthread_cond_broadcast(&m_pthreadCond);
for(int i = 0; i < m_iThreadNum; ++ i)
{
pthread_join(pthread_id[i], NULL);
}
// delete[] pthread_id;
// pthread_mutex_destroy(&m_pthreadMutex);
// pthread_cond_destroy(&m_pthreadCond);
return 0;
}
/**
@ 返回当前任务列表里面的任务数量
*/
int CThreadPool::getTaskSize()
{
return this->m_vecTaskList.size();
}
/**
@ 创建线程
@ 成功返回0
*/
int CThreadPool::CreatePool()
{
this->pthread_id = new pthread_t(m_iThreadNum);
for(int i = 0; i < this->m_iThreadNum; ++ i)
{
pthread_create(&pthread_id[i], NULL, ThreadFunc, NULL);
}
return 0;
}
void *CThreadPool::ThreadFunc(void *arg)
{
pthread_mutex_lock(&m_pthreadMutex);
pthread_t tid = pthread_self();
pthread_mutex_unlock(&m_pthreadMutex);
cout << tid << endl;
while(1)
{
pthread_mutex_lock(&m_pthreadMutex);
while(m_vecTaskList.size() == 0 && !shutdown)
{
cout << "cond_wait id is:" << tid << endl;
pthread_cond_wait(&m_pthreadCond, &m_pthreadMutex);
}
if(shutdown)
{
cout << "thread " << tid << " exit" << endl;
pthread_mutex_unlock(&m_pthreadMutex);
pthread_exit(NULL);
}
vector<CTask *>::iterator iter = m_vecTaskList.begin();
CTask *temp = NULL;
if(iter != m_vecTaskList.end())
{
temp = *iter;
m_vecTaskList.erase(iter);
}
pthread_mutex_unlock(&m_pthreadMutex);
temp->run();
}
pthread_exit(NULL);
}
int main (void)
{
cout << "begin............." << endl;
CThreadPool pool(5);
for(int i = 0; i < 50; ++ i)
{
CTask *task = new CTask();
pool.AddTask(task);
}
while(pool.getTaskSize() != 0)
{
cout << pool.getTaskSize() << endl;
continue;
}
pool.StopAll();
cout << "end..............." << endl;
return 0;
}