void main()
{
handleMutex=::CreateEvent(NULL,true,true,NULL);
CloseHandle(handleMutex);
.
.
.}
DWORD WINAPI ThreadFun1(LPVOID pParam)
{while(1)
{
::WaitForSingleObject(handleMutex,INFINITE);
ResetEvent(handleMutex);
//由于没有用SetEvent(handleMutex);程序是否一直在这运行?} //end while}
DWORD WINAPI ThreadFun2(LPVOID pParam)
{while(1)
{
::WaitForSingleObject(handleMutex,INFINITE);
ResetEvent(handleMutex);SetEvent(handleMutex);
} // end while
}24分,我的全部啊欲哭无泪啊。
{
handleMutex=::CreateEvent(NULL,true,true,NULL);
CloseHandle(handleMutex);
.
.
.}
DWORD WINAPI ThreadFun1(LPVOID pParam)
{while(1)
{
::WaitForSingleObject(handleMutex,INFINITE);
ResetEvent(handleMutex);
//由于没有用SetEvent(handleMutex);程序是否一直在这运行?} //end while}
DWORD WINAPI ThreadFun2(LPVOID pParam)
{while(1)
{
::WaitForSingleObject(handleMutex,INFINITE);
ResetEvent(handleMutex);SetEvent(handleMutex);
} // end while
}24分,我的全部啊欲哭无泪啊。
CloseHandle(handleMutex);
刚创建你就关闭了handleMutex句柄了?WaitForSingleObject在一个无效的句柄上等待了.永远不会返回了
主线程里面生成马上又close了,子线程肯能会出现不可预测的结果............
很不幸的是,程序运行到WaitForSingleObject会继续运行下去啊
{
handleMutex=::CreateEvent(NULL,true,true,NULL);
CloseHandle(handleMutex);
.
.
. }
DWORD WINAPI ThreadFun1(LPVOID pParam)
{ while(1)
{
::WaitForSingleObject(handleMutex,INFINITE);
ResetEvent(handleMutex);
//由于没有用SetEvent(handleMutex);程序是否一直在这运行? } //end while }
DWORD WINAPI ThreadFun2(LPVOID pParam)
{ while(1)
{
::WaitForSingleObject(handleMutex,INFINITE);
ResetEvent(handleMutex); SetEvent(handleMutex);
} // end while
} 24分,我的全部啊欲哭无泪啊。
我想问程序是否会一直在线程一运行?
很显然程序会一直运行下去,因为人家等的是个无效的句柄。它可以认为它等待的事件是发生的,就一直运行了。它也可以认为等待的事件未发生,一直等待,就看它自己决定的了,哈哈,开玩笑的。其实MSDN说的
很明白:
If this handle is closed while the wait is still pending, the function's behavior is undefined.
也就是你要是在线程还在等待的时候,你提前关闭了句柄,那么WaitForSingleObject的返回是不确定的,所谓不确定就是我上面说的意思
其实,你的意思可能是有信号了退出线程吧??那么你应该在WaitForSingleObject之后加上return ;
我的意思是,由于信号在未通知状态(没有设SetEvent),所以别的线程就不能运行了。如果没有设SetEvent,而其它每个线程都有开头都有WaitForSingleObject,是否其它线程都不能运行?
必须手动设置SetEvent,其他的线程才能返回
我在非主线程里没设SetEvent,但主线程的OnTimer一样可以运行。
ontime()
{
WaitForSingleObject
}