Why is Sleep(1000) resulting in a deadlock?
I have a basic Win32 console app that makes a call to a named pipe, and then Sleep(1000), inside a while(true) loop. After a hundred iterations, Sleep(1000) will hang. I can see no reason for this.
Ok, all I'm doing is taking the code found in this MSDN sampl开发者_开发技巧e, verbatim and running it as the server: http://msdn.microsoft.com/en-us/library/aa365601%28VS.85%29.aspx
Then I take the code found in this MSDN sample for the client found here: http://msdn.microsoft.com/en-us/library/aa365592%28VS.85%29.aspx, and I modify it so that it moves all of the calling code into a separate method, and then from within main() calls it like this:
while (true)
{
sendmsg();
Sleep(1000);
}
I should add that the problem happens when I run multiple instances of the client, say around 4 or 5 instances. I can see no reason for this. There is no synchronization at all taking place. The code is exactly as is found in the two links, save for the change to run the client code in a loop, with a Sleep(1000) after each call.
Sleep cannot hang. Watch out for debugger behavior, it tends to put the green arrow on the next statement if it doesn't have any source code for the active code. Seeing a pipe call hang would be quite normal. After Debug + Break All, be sure to scroll the stack trace up to frames in the operating system code.
So, one (or more?) of your clients blocks when multiple clients are running and connecting and sending in the tight loop shown?
Break into the code when it has blocked and post the stack trace.
You're sure you're not blocking in the 20 second wait that occurs in the sample code when all pipe instances are busy? I don't think this is likely, but it's possible...
It's probably best for you to post the exact code you're using. You may have made a mistake in how you moved the code into the function you're calling and you might be leaking resources or something which is later causing you problems.
Since you're running multiple clients, it might be useful to output a loop counter from each one to show how many iterations they each do before you get your problem; perhaps it's always the same number of total iterations? Do you hang more quickly with more clients? Less quickly with fewer clients?
精彩评论