C++ 在Windows中加入等效项
如何在Windows中等待线程消亡?我希望我的代码是这样的:C++ 在Windows中加入等效项,c++,windows,multithreading,winapi,join,C++,Windows,Multithreading,Winapi,Join,如何在Windows中等待线程消亡?我希望我的代码是这样的: main thread: creating thread: thread1 waiting for thread1 to die //rest of the code 我正在使用Win32 API。这很简单:WaitForSingleObject可以在给定另一个线程句柄的情况下阻止当前线程 void Thread1Proc() { HANDLE hThread2 = CreateThread(...); WaitFor
main thread:
creating thread: thread1
waiting for thread1 to die
//rest of the code
我正在使用Win32 API。这很简单:WaitForSingleObject可以在给定另一个线程句柄的情况下阻止当前线程
void Thread1Proc()
{
HANDLE hThread2 = CreateThread(...);
WaitForSingleObject(hThread2, INFINITE);
// by now thread #2 is over
}
这就是你想要的,你真正想要的吗?尽量不要这样做。使用循环且从不终止的线程池或应用程序线程的生存期。等待线程终止应该是最后的手段,只有在没有其他设计方法可以工作时才使用。