您现在的位置是:首页 > 文章详情

Windows10 VS2017 C++多线程传参和等待线程结束

日期:2018-12-27点击:595
版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/85322164
 #include "pch.h" #include <iostream> #include <windows.h> using namespace std; typedef struct MyData { const char* str; }MYDATA; //线程函数 DWORD WINAPI Fun(LPVOID lpParamter) { MYDATA *pmd = (MYDATA *)lpParamter; for (int i = 0; i < 10; i++) { cout << "Displaying " << pmd->str << endl; Sleep(500); } return 0; } int main() { //使用struct传递参数 MYDATA xstr; xstr.str = "你好!"; //使用GetExitCodeThread()轮询检查 //DWORD exitCode = 0; //HANDLE hThread = CreateThread(NULL, 0, Fun, &xstr, 0, NULL); //while (1) { // GetExitCodeThread(hThread, &exitCode); // 严重浪费 CPU 时间 // if (STILL_ACTIVE != exitCode) // break; //} //CloseHandle(hThread); //WaitForSingleObject(),cpu使用率极低 HANDLE hThread = CreateThread(NULL, 0, Fun, &xstr, 0, NULL); WaitForSingleObject(hThread, INFINITE); // 等待,直到线程被激发 CloseHandle(hThread); cout << "Child thread is over." << endl; return 0; } 

参考文章:
https://www.cnblogs.com/XiHua/p/5028329.html

原文链接:https://yq.aliyun.com/articles/683164
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章