WaitForSingleObject and while loops in C/++
Here is the snippet: prog1:
HANDLE hM;
hM = CreateMutexA(NULL,TRUE, "abc"); // I have to use TRUE otherwise WaitForSingleObject fails.. by design it wants to take ownership and w//o TRUE there is no owner anyway right? <<-- **check this please if its true**
...
prog2:
HANDLE hM;
hM = OpenMutexA(MUTEX_ALL_ACCESS,NULL, "abc");
while(WaitForSingleObject(hM,INFINITE)) {
// do smthing
ReleaseMutex(hM);
hM = OpenMutexA(MUTEX_ALL_ACCESS,NULL, "abc"); // In order to continue while loop but it doesnt rewind,
}
Issue> It won't make semi-infinite loop ... it enters once and thats it.
Purpose? To monitor activity of some process (if it crash I restart it), infinite loops are hitting my CPU hard and I cant use Sleep, I need to restart it asasp (well if some smart solution is possible via while(1) or for(;;) please post)
UPDATE
should be while(WaitForSingleObject(hM,INFINITE) == WAIT_ABANDONED){ ... }, I didnt copy correctly :).
It works once and then it won't wait again, is there a way to reset handle?
prog1:
HANDLE hM;
hM = CreateMutexA(NULL,TRUE, "abc");
prog2:
HANDLE hM;
hM = OpenMutexA(MUTEX_ALL_ACCESS,NULL, "abc");
while(WaitForSingleObject(hM,INFINITE)) {
ReleaseMutex(hM);
Sleep(1000);
CloseHandle(hM)
// do smthing like CreateProcess()
Sleep(1000);
hM = OpenMutexA(MUTEX_ALL_ACCESS,NULL, "abc");
}
This way it works, if anyone wants to add something comment on this post.
To detect process crash, you shouldn't bother with a mutex. Just wait on the process handle. Once the process exits, the handle will be signaled, and WaitForSingleObject
will return.
If you're creating the process, you can get the process handle from the PROCESS_INFORMATION
argument. Then, just use:
PROCESS_INFORMATION pi;
do {
// create the process, passing pi as the last argument to CreateProcess
} while (WaitForSingleObject(pi.hProcess, INFINITE));
Note that the handle should be closed, you can figure out when.
Update - Given this is not an option, here's the problem in your code:
Say prog1 started successfully, and prog2 is now waiting for the mutex to be signaled. Then prog1 crashes, and prog2 gets to own the mutex. Given prog2's purpose is to relaunch prog1, I assume this is what happens in // do smthing
. So now prog1 starts, and calls CreateMutexA
. But prog2 might still be holding the mutex, and so prog1 only gets a handle to the existing mutex, but it doesn't actually own it. Now prog2 calls ReleaseMutex, and continues to a wait on a signaled mutex, which is not owned by prog1. The wait immediately returns with WAIT_OBJECT_0
, and you get out of the while
.
So, I think prog1's use of CreateMutexA
is wrong here. You should instead create an un-owned mutex, and wait on it. This way, you'll ensure prog1's ownership of the mutex.
I agree with eran's point that you should just wait directly on the process handle.
As to why your code does not enter the while
loop when you think it should that is easy enough to work out. The while
loop terminates when WaitForSingleObject
returns 0. That is when it returns WAIT_OBJECT_0
, or in other words when it has acquired ownership of the mutex. That happens when the previous owner releases it.
You don't need to reopen the mutex. WaitForSingleObject takes ownership and ReleaseMutex releases the ownership, but the mutex stays open until you call CloseHandle.
链接地址: http://www.djcxy.com/p/62468.html