windows下怎么等待线程结束
有三个方法
假设
thread = CreateThread(0,0,thread_func,...)
UINT thread_fun(void* f)
{
while(1) do ...
1. g_thread_exit = true
2. SetEvent(thread_exit)
3. ... do nothing
}
相对的,主线程的等待是
1. while(!g_thread_exit)
2. WaitForSingleObject(thread_exit)
3. WaitForSingleObject(thread)
其中1,2类似,3才是最完美的,因为1,2只是事件发生点,可线程本身还在执行,比如thread_fun中c++变量的析构,
3才是真正结束点。