线程内核对象
当线程正在运行时,线程内核对象处于未通知状态。当线程停止运行时,就处于已通知状态。可以通过等待线程来检查线程是否仍然运行。
无成功等待的副作用。
// 使用beginThread创建一个线程,等待线程退出
#include <process.h>
#include <windows.h>
#include <iostream>
struct MYdataValue
{
MYdataValue(int d1,double d2):
a(d1),
b(d2)
{}
int a;
double b;
};
class CThreadCls
{
public:
static unsigned int __stdcall first(void* p);
};
unsigned int __stdcall CThreadCls::first(void* p)
{
MYdataValue* pThis = (MYdataValue*)p;
std::cout << "this is CThreadCls::first thread! num = "<<pThis->a<<"double = "<<pThis->b << std::endl;
delete pThis;
Sleep(5000);
return 0;
}
int main()
{
unsigned int d1;
HANDLE handle;
MYdataValue* pData = new MYdataValue(1,3.22);
handle = (HANDLE)_beginthreadex(nullptr,0, &CThreadCls::first, pData,0,&d1);
if (WAIT_OBJECT_0 == WaitForSingleObject(handle, INFINITE))//等待子线程结束
{
std::cout << "thread excute end!" << std::endl;
}
std::cout << "handle = " << handle << std::endl;
std::cout << "threadid = " << d1 << std::endl;
CloseHandle(handle);
return 0;
}