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才是真正结束点。

posted on 2011-09-01 09:35  风行水上  阅读(2071)  评论(0编辑  收藏  举报