views:

106

answers:

2

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 sample, 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.

+1  A: 

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.

Hans Passant
Well with that said, does anyone see a reason why the code that is in the links I provided, by Microsoft, would hang when multiple clients are connecting?
Leeks and Leaks
I doubt the linked code hangs. Yours probably does because you are not reading the response from the server anymore. The pipe buffer will fill up to capacity, then it hangs waiting for the buffer to drain.
Hans Passant
Well I am running the exact same code that I linked to, including the reading of the pipe by the client, after it sends its message. In fact all I've changed is that I've moved the code within the client example into a separate function called sendmsg(), and then I simply call it repeatedly in the while loop, sleeping after each call. If you already understood that and still think I'm missing your point, please let me know, I just wanted to be clear about what I'm doing. I can post the entire client code, it's nearly identical to what I linked to.
Leeks and Leaks
+1  A: 

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?

Len Holgate