本文实例讲述了C++开启线程CreatThread函数的使用,实现主线程与工作线程交互的功能。分享给大家供大家参考。
具体实现代码如下:
复制代码 代码如下:
//线程函数
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
for (int i=0;i<20;i++)
{
printf("I'm in thread,count=%d\n",i);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwThreadId;
HANDLE hThread = ::CreateThread(NULL, 0, ThreadProc,NULL, 0, &dwThreadId);
if (hThread == NULL)
{
printf("createThread failed...");
return -1;
}
//等待线程结束
::WaitForSingleObject(hThread, INFINITE);
printf("thread finished...");
::CloseHandle(hThread);
return 0;
}
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
for (int i=0;i<20;i++)
{
printf("I'm in thread,count=%d\n",i);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwThreadId;
HANDLE hThread = ::CreateThread(NULL, 0, ThreadProc,NULL, 0, &dwThreadId);
if (hThread == NULL)
{
printf("createThread failed...");
return -1;
}
//等待线程结束
::WaitForSingleObject(hThread, INFINITE);
printf("thread finished...");
::CloseHandle(hThread);
return 0;
}
希望本文所述对大家的C++程序设计有所帮助。