C++线程退出现错误的原因

wanwan_1996 2017-06-09 10:40:50

在测试函数中调用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;
}
...全文
664 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhujinqiang 2017-06-09
  • 打赏
  • 举报
回复
zhujinqiang 2017-06-09
  • 打赏
  • 举报
回复
VC++ 2010? 2013? 参考: http://blog.csdn.net/jcjc918/article/details/50395528 ThreadPool是一个轻量级,通用,纯C++11 线程池,需要编译器实现了C++11的特性

1,314

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧