Files
25Sep01_ThreadBase/code/ThreadBase.cpp

93 lines
2.5 KiB
C++

#include "ThreadBase.h"
ThreadBase::ThreadBase(void)
{
p_tThreadCnt = 0;
m_tThreadPid_ = 0;
}
ThreadBase::~ThreadBase()
{
}
void *ThreadBase::ThreadBase_ThreadLoopDistribute(void *pInputArg)
{
ThreadParam *pTempParam = (ThreadParam *)pInputArg;
ThreadBase *pTempThread = (ThreadBase *)(pTempParam->m_pInstance_);
pTempThread->ThreadBase_ThreadLoopFunc(pTempParam->m_tFlag_, pTempParam->m_pArg_);
free(pTempParam);
std::cout << "ThreadExit 子线程已退出" << std::endl;
pthread_detach(pthread_self());
std::cout << "ThreadExit 子线程已分离" << std::endl;
return NULL;
}
void ThreadBase::ThreadBase_ThreadJoin(void)
{
if (m_tThreadPid_ != 0)
pthread_join(m_tThreadPid_, NULL);
}
int ThreadBase::ThreadBase_ThreadCreate(void)
{
return ThreadBase_InterCreateThread(-1, 0, NULL);
}
int ThreadBase::ThreadBase_ThreadCreate(void *pInputArg)
{
return ThreadBase_InterCreateThread(-1, 0, pInputArg);
}
int ThreadBase::ThreadBase_ThreadCreate(int tInputFlag, void *pInputArg)
{
return ThreadBase_InterCreateThread(-1, tInputFlag, pInputArg);
}
int ThreadBase::ThreadBase_ThreadCreateWithPrio(int tInputPrio)
{
return ThreadBase_InterCreateThread(tInputPrio, 0, NULL);
}
int ThreadBase::ThreadBase_InterCreateThread(int tInputPrio, int tInputFlag, void *pInputArg)
{
ThreadParam *pTempParam;
pTempParam = (ThreadParam *)malloc(sizeof(ThreadParam));
if (pTempParam == NULL)
{
return 0;
}
p_tThreadCnt++;
pTempParam->m_pInstance_ = this;
pTempParam->m_tFlag_ = tInputFlag;
pTempParam->m_pArg_ = pInputArg;
#ifdef dUseExternLib2
if (tInputPrio >= 0 && MergeClient_GetChipType() == dMergeChipType_Rtd1296)
{
pthread_attr_t tTempAttr;
struct sched_param tTempParam;
int policy;
pthread_attr_init(&tTempAttr);
pthread_attr_getschedpolicy(&tTempAttr, &policy);
if (policy != SCHED_RR)
pthread_attr_setschedpolicy(&tTempAttr, SCHED_RR);
tTempParam.sched_priority = tInputPrio;
pthread_attr_setschedparam(&tTempAttr, &tTempParam);
pthread_create(&(pTempParam->m_tThreadID_), (tInputPrio >= 0) ? (&tTempAttr) : NULL, ThreadBase_ThreadLoopDistribute, pTempParam);
}
else
#else
(void)tInputPrio;
#endif
{
pthread_create(&(pTempParam->m_tThreadID_), NULL, ThreadBase_ThreadLoopDistribute, pTempParam);
}
m_tThreadPid_ = pTempParam->m_tThreadID_;
return (int)pTempParam->m_tThreadID_;
}