新增:实现 ThreadBase 类,创建单个线程

This commit is contained in:
2025-09-01 17:54:23 +08:00
commit 390348ab97
9 changed files with 344 additions and 0 deletions

33
code/main.cpp Normal file
View 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;
}