pthread_cond_signal 段错误

Sander 2009-08-20 04:37:32
我写了个lib, 封装了一个线程类
#include "ExThread_Linux.h"
#include <sys/types.h>
#include <unistd.h>

#define EXIT_THREAD_TIMEOUT 2000//2second
//--------------------------------------------------------------------------------------------
CExThread_Linux::CExThread_Linux(void* pCallback, void* pOwner)
{
m_pCallbackFunc = (CallBackDoWithData)pCallback;
m_pOwner = pOwner;

m_bCond = false;
pthread_mutex_init(&m_mutex, NULL);
pthread_cond_init(&m_cond, NULL);
m_bThreadStart = true;

}

CExThread_Linux::~CExThread_Linux()
{
DestroyThread();
}

//--------------------------------------------------------------------------------------------
int CExThread_Linux::DestroyThread()
{
if (m_thread_id)
{
m_bThreadStart = false;
SetSignal();
pthread_join(m_thread_id, NULL);
pthread_mutex_destroy(&m_mutex);
pthread_cond_destroy(&m_cond);
}

return 1;
}

//--------------------------------------------------------------------------------------------
bool CExThread_Linux::CreateThread()
{
int ret = 0;

pthread_attr_t attr;
sched_param param;

ret = pthread_create(&m_thread_id, NULL, Thread, this);
if(ret!=0)
{
printf("create pushthread error!\n");
return false;
}

int n_MaxPriority = 0;
sched_get_priority_max(n_MaxPriority);
pthread_attr_getschedparam(&attr, ¶m);
param.sched_priority = n_MaxPriority;
pthread_attr_setschedparam(&attr, ¶m);

return true;
}

void CExThread_Linux::SetSignal()
{
pthread_cond_signal(&m_cond);
//printf("SetSignal-------d\r\n");

}

//--------------------------------------------------------------------------------------------
bool CExThread_Linux::SetEventAndTimeout(bool bCondition, const struct timeval* tv)
{
m_bCond = bCondition;
if (!m_bCond && (struct timeval*)tv==NULL)
return false;

m_timeout = (struct timeval*)tv;
return true;
}

//--------------------------------------------------------------------------------------------
void* CExThread_Linux::Thread( void* lParam )
{
int nRetVal = 0;
int nCallBackResult = 0;
struct timeval now;

CExThread_Linux* pThis = ( CExThread_Linux* )lParam;
if( !pThis )
return 0;

if (!pThis->m_bCond)
{
while(true)
{
now.tv_sec = pThis->m_timeout->tv_sec;
now.tv_usec = pThis->m_timeout->tv_usec;
int nRetVal = select( 0, NULL , NULL , NULL , &now);
if ( -1 == nRetVal )
{
printf( "CListonSocket::Select Error\n" );
return false;
}

pThis->m_pCallbackFunc(pThis->m_pOwner);
}
}
else
{
while (true)
{
pthread_mutex_lock(&(pThis->m_mutex));
pthread_cond_wait(&(pThis->m_cond),&(pThis->m_mutex));
pthread_mutex_unlock(&(pThis->m_mutex));
if (!pThis->m_bThreadStart)
break;

if ( pThis->m_pCallbackFunc )
{
pThis->m_pCallbackFunc(pThis->m_pOwner);
}

}

}

return 0;
}
...全文
131 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sander 2009-09-07
  • 打赏
  • 举报
回复
找到原因了
Sander 2009-08-24
  • 打赏
  • 举报
回复
顶下
Sander 2009-08-21
  • 打赏
  • 举报
回复
//ExThread2->SetSignal();
这个出错
ShowMan 2009-08-20
  • 打赏
  • 举报
回复
怎么确定是pthread_cond_signal 报的段错误。
m_cond的初始值是啥呢?
xwb2766 2009-08-20
  • 打赏
  • 举报
回复
测试一下
Sander 2009-08-20
  • 打赏
  • 举报
回复
demo程序如下:
===============================================
#include "stdio.h"
#include <unistd.h>
#include "ExThread_Linux.h"
static void ProcData(void*);
static void ProcData2(void*);

static CExThread_Linux* ExThread = NULL;
static CExThread_Linux* ExThread2 = NULL;

int main()
{

CExThread_Linux* ExThread2 = new CExThread_Linux((void*)ProcData2, NULL);
ExThread2->SetEventAndTimeout(true, 0);
ExThread2->CreateThread();

printf("\nIf you want Quit the programme, please press q key and return key: \n");

sleep(5);

CExThread_Linux* ExThread = new CExThread_Linux((void*)ProcData, NULL);
timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;

ExThread->SetEventAndTimeout(false, &tv);
ExThread->CreateThread();

int a;
bool bIsDone = false;
while(!bIsDone)
{
ExThread2->SetSignal();
//scanf("%c")!=1) //¼ì²âÊÇ·ñΪÕýÈ·ÊäÈëÊý
a = getchar();
switch(a)
{
case 'q':
case 'Q':
bIsDone = true;
break;
case 'g':
case 'G':
//pServer->Call();
break;
default:
break;
}
}

return 0;

}



void ProcData(void* pParam)
{
static int nCounter = 0;

nCounter++;
ExThread2->Resume();
//ExThread2->SetSignal();
printf("Timeout Counter number:%d\r\n", nCounter);


}

void ProcData2(void* pParam)
{
static int nCounter = 0;

nCounter++;
printf("Signal Counter number:%d\r\n", nCounter);


}

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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