Re: CInternetSession

Tech-Archive recommends: Fix windows errors by optimizing your registry



On Mon, 28 May 2007 00:37:10 -0400, Joseph M. Newcomer
<newcomer@xxxxxxxxxxxx> wrote:

I didn't think it did. What I see is a poor design for an appliction that goes out and
polls a Web site every 10 seconds or so.

The presence of the Sleep(), however, would represent a bad design if the purpose is to
poll the Web site every 10 seconds. If it represents ten seconds of computing, then the
design is just as bad.

If the Sleep() is gratuitous, the design is even worse, because it is still means the loop
blocks the message pump indefinitely, either compute bound or I/O blocked. In either
case, the design is still bad.
joe

The Sleep didn't appear in the loop in his original post. It appeared in a
second version he posted, which he modified to comply with a suggestion he
got (which did not include adding a Sleep call). In that message, he did
mention observing the memory leak in Task Manager. If you're trying to use
Task Manager to observe a steady memory leak in a loop, adding a Sleep call
is the easiest way to slow things down so that you can monitor it, so I
figured that's what he was doing; it certainly did not appear he added it
to attempt to "fix" his code. Of course, you're right that using Sleep like
this to implement an actual algorithm is a lousy way to do it[*], but as a
quick, dirty, non-robust debugging or demonstration aid, it can be useful.
For example, I used it in this message to "eliminate" (note the presence of
the quotes) a race in an example program that demonstrated that a secondary
thread doesn't keep a C++ application alive once the primary thread has
terminated:

http://groups.google.com/group/microsoft.public.vc.mfc/msg/329b0220c98a34a2

I did comment it with:

// Sleeping here is a non-robust way to address
// the race condition.
Sleep(5000);

In a real program, I would have eliminated (note the absence of the quotes)
the race condition, but this was good enough for the demo.

[*] Then again, if it were a console application whose sole purpose in life
was to poll a web site and record its up-time to a file, using Sleep might
be a fine way to do it. This can be said only rarely, though, and it
shouldn't be used this way in a GUI app that aspires to be robust. (Roughly
seven years ago, I converted the MFC TEAR console program example into an
up-time program by using Sleep to monitor the crummy ISP I was using at the
time; it worked perfectly, but my design considerations included (a) "no
user interaction" and (b) "runs forever". With those constraints, just
about anything goes. <g>)

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: CInternetSession
    ... the presence of the Sleepindicates the serious design flaw. ... correct model would be to simply use an OnTimer event that would trigger the next ... It never ceases to amaze me how programmers try to cram everything into the single-thread ... Sleep() calls around like pixie dust, your design is fundamentally broken and will need to ...
    (microsoft.public.vc.mfc)
  • Re: Zephram can okay borrow their slave
    ... out rob. ... It might jump ill resorts, do you design them? ... sleep them, then we very enforce Bernadette and Vance's important ...
    (sci.crypt)
  • Re: AfxBeginThread startup times and overhead
    ... the correct solution would be ... failures under load, lost packets, hangs, etc. stemmed from bad design like this. ... I took one look at the code, saw a Sleep() inside it, and said "This code will lose ... This is in direct contrast to waiting on a mutex, event, ...
    (microsoft.public.vc.mfc)
  • Re: CInternetSession
    ... polls a Web site every 10 seconds or so. ... The presence of the Sleep, however, would represent a bad design if the purpose is to ... If the Sleep() is gratuitous, the design is even worse, because it is still means the loop ... It never ceases to amaze me how programmers try to cram everything into the single-thread ...
    (microsoft.public.vc.mfc)
  • Re: this.wait( sleep );
    ... The confusion is not the result from ... > interrupt, yield, sleep, ... ... it does mean that the Thread class is entirely ... That would certainly not be your design. ...
    (comp.lang.java.programmer)