新增:创建多个线程
This commit is contained in:
@ -7,11 +7,12 @@ public:
|
||||
void ThreadBase_ThreadLoopFunc(int tInputFlag, void *pInputArg) override
|
||||
{
|
||||
std::string *pTempMSG = (std::string *)pInputArg;
|
||||
for (int tTempN = 0; tTempN < 5; tTempN++)
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
std::cout << "[子线程] flag=" << tInputFlag
|
||||
<< " pTempMSG=" << *pTempMSG
|
||||
<< " tTempN=" << tTempN << std::endl;
|
||||
std::cout << "[子线程 " << pthread_self()
|
||||
<< "] flag=" << tInputFlag
|
||||
<< " msg=" << *pTempMSG
|
||||
<< " i=" << i << std::endl;
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
@ -19,15 +20,23 @@ public:
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
MyThread tTempThread;
|
||||
std::string tTempString = "HelloThread";
|
||||
// 创建多个线程对象
|
||||
MyThread tTempThread1, tTempThread2, tTempThread3;
|
||||
std::string tTempString1 = "线程一";
|
||||
std::string tTempString2 = "线程二";
|
||||
std::string tTempString3 = "线程三";
|
||||
|
||||
/* 启动线程 */
|
||||
std::cout << "[主线程] 创建线程..." << std::endl;
|
||||
tTempThread.ThreadBase_ThreadCreate(123, &tTempString);
|
||||
/* 等待子线程退出 */
|
||||
tTempThread.ThreadBase_ThreadJoin();
|
||||
std::cout << "[主线程] 线程已退出" << std::endl;
|
||||
std::cout << "[主线程] 创建子线程..." << std::endl;
|
||||
|
||||
tTempThread1.ThreadBase_ThreadCreate(101, &tTempString1);
|
||||
tTempThread2.ThreadBase_ThreadCreate(202, &tTempString2);
|
||||
tTempThread3.ThreadBase_ThreadCreate(303, &tTempString3);
|
||||
|
||||
// 等待所有线程退出
|
||||
tTempThread1.ThreadBase_ThreadJoin();
|
||||
tTempThread2.ThreadBase_ThreadJoin();
|
||||
tTempThread3.ThreadBase_ThreadJoin();
|
||||
|
||||
std::cout << "[主线程] 所有子线程已退出" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user