新增:实现 ThreadBase 类,创建单个线程
This commit is contained in:
93
code/ThreadBase.cpp
Normal file
93
code/ThreadBase.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
#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_;
|
||||
}
|
||||
38
code/ThreadBase.h
Normal file
38
code/ThreadBase.h
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef THREADBASE_H
|
||||
#define THREADBASE_H
|
||||
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
|
||||
class ThreadBase
|
||||
{
|
||||
public:
|
||||
ThreadBase(void);
|
||||
virtual ~ThreadBase();
|
||||
virtual void ThreadBase_ThreadLoopFunc(int tInputFlag, void *pInputArg) = 0;
|
||||
// virtual void ThreadBase_ThreadLoopFunc(int tInputFlag, void *pInputArg) {}
|
||||
static void *ThreadBase_ThreadLoopDistribute(void *pInputArg);
|
||||
void ThreadBase_ThreadJoin(void);
|
||||
int ThreadBase_ThreadCreate(void);
|
||||
int ThreadBase_ThreadCreate(void *pInputArg);
|
||||
int ThreadBase_ThreadCreate(int tInputFlag, void *pInputArg);
|
||||
int ThreadBase_ThreadCreateWithPrio(int tInputPrio);
|
||||
|
||||
private:
|
||||
struct ThreadParam
|
||||
{
|
||||
pthread_t m_tThreadID_;
|
||||
void *m_pInstance_;
|
||||
int m_tFlag_;
|
||||
void *m_pArg_;
|
||||
};
|
||||
pthread_t m_tThreadPid_;
|
||||
|
||||
int ThreadBase_InterCreateThread(int tInputPrio, int tInputFlag, void *pInputArg);
|
||||
|
||||
protected:
|
||||
int p_tThreadCnt;
|
||||
};
|
||||
|
||||
#endif
|
||||
33
code/main.cpp
Normal file
33
code/main.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "ThreadBase.h"
|
||||
|
||||
/* 定义一个继承类,重写 ThreadBase_ThreadLoopFunc */
|
||||
class MyThread : public ThreadBase
|
||||
{
|
||||
public:
|
||||
void ThreadBase_ThreadLoopFunc(int tInputFlag, void *pInputArg) override
|
||||
{
|
||||
std::string *pTempMSG = (std::string *)pInputArg;
|
||||
for (int tTempN = 0; tTempN < 5; tTempN++)
|
||||
{
|
||||
std::cout << "[子线程] flag=" << tInputFlag
|
||||
<< " pTempMSG=" << *pTempMSG
|
||||
<< " tTempN=" << tTempN << std::endl;
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
MyThread tTempThread;
|
||||
std::string tTempString = "HelloThread";
|
||||
|
||||
/* 启动线程 */
|
||||
std::cout << "[主线程] 创建线程..." << std::endl;
|
||||
tTempThread.ThreadBase_ThreadCreate(123, &tTempString);
|
||||
/* 等待子线程退出 */
|
||||
tTempThread.ThreadBase_ThreadJoin();
|
||||
std::cout << "[主线程] 线程已退出" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user