当前位置: 技术问答>linux和unix
pthread_cond_signal 段错误
来源: 互联网 发布时间:2016-06-29
本文导语: 我写了个lib, 封装了一个线程类 #include "ExThread_Linux.h" #include #include #define EXIT_THREAD_TIMEOUT 2000//2second //-------------------------------------------------------------------------------------------- CExThread_Linux::CEx...
我写了个lib, 封装了一个线程类
#include "ExThread_Linux.h"
#include
#include
#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-------drn");
}
//--------------------------------------------------------------------------------------------
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 Errorn" );
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;
}
#include "ExThread_Linux.h"
#include
#include
#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-------drn");
}
//--------------------------------------------------------------------------------------------
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 Errorn" );
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;
}
|
测试一下
|
怎么确定是pthread_cond_signal 报的段错误。
m_cond的初始值是啥呢?
m_cond的初始值是啥呢?
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。