新增:创建多个线程
This commit is contained in:
10
README.md
10
README.md
@ -1,5 +1,11 @@
|
|||||||
V1.1.0
|
## V1.1.0
|
||||||
|
|
||||||
### 新增:
|
### 新增:
|
||||||
|
|
||||||
实现`ThreadBase`类,创建单个线程
|
实现 `ThreadBase`类,创建单个线程
|
||||||
|
|
||||||
|
## V1.1.0
|
||||||
|
|
||||||
|
### 新增:
|
||||||
|
|
||||||
|
创建多个线程
|
||||||
|
|||||||
@ -7,11 +7,12 @@ public:
|
|||||||
void ThreadBase_ThreadLoopFunc(int tInputFlag, void *pInputArg) override
|
void ThreadBase_ThreadLoopFunc(int tInputFlag, void *pInputArg) override
|
||||||
{
|
{
|
||||||
std::string *pTempMSG = (std::string *)pInputArg;
|
std::string *pTempMSG = (std::string *)pInputArg;
|
||||||
for (int tTempN = 0; tTempN < 5; tTempN++)
|
for (int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
std::cout << "[子线程] flag=" << tInputFlag
|
std::cout << "[子线程 " << pthread_self()
|
||||||
<< " pTempMSG=" << *pTempMSG
|
<< "] flag=" << tInputFlag
|
||||||
<< " tTempN=" << tTempN << std::endl;
|
<< " msg=" << *pTempMSG
|
||||||
|
<< " i=" << i << std::endl;
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -19,15 +20,23 @@ public:
|
|||||||
|
|
||||||
int main(int argc, char **argv)
|
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;
|
||||||
std::cout << "[主线程] 创建线程..." << std::endl;
|
|
||||||
tTempThread.ThreadBase_ThreadCreate(123, &tTempString);
|
|
||||||
/* 等待子线程退出 */
|
|
||||||
tTempThread.ThreadBase_ThreadJoin();
|
|
||||||
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;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user