Re: AfxBeginThread startup times and overhead

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/30/04


Date: Sun, 30 May 2004 19:57:19 -0400

Bad design. the correct solution would be

::WaitForSingleObject(flag, INFINITE);

where flag is now a HANDLE to an Event created by ::CreateEvent.

All you've done is create a remarkably inefficient spin lock, with all of its
disadvantages.

Of course, the presence of the Sleep(n) is a dead giveaway. A proper solution must be both
correct AND CPU-efficient without sleeping.

Letting this sort of thing creep into your code produces long-term maintenance nightmares.
One of my few recent failures was code that had things like this all over it, and I had to
tell the client that the code was unsalvageable--all the bugs they reported, including
failures under load, lost packets, hangs, etc. stemmed from bad design like this. When
they called me in to examine the code and do a walkthrough, they showed me one subroutine.
I took one look at the code, saw a Sleep() inside it, and said "This code will lose
packets under heavy load conditions, lose connections, hang connections, and occasionally
the app will just come to a screeching halt and you'll have to restart it". Without seeing
more than the one Sleep() call, and the five-line loop that contained it. They said "Yes,
that's exactly what we're seeing! How did you know that?" and the answer was "I saw a
Sleep() in the code. This means the design is deeply flawed" (It was worse than that.
There were sleep calls inside mutexes, there were mutexes because the programming had
apparently never heard of malloc or new and used a single global buffer, there were
mutexes set before blocking I/O calls, etc. but I didn't know that at the time I first
examined the code). After spending a week looking at the code, I realized that this sort
of thing was so pervasive, and the code so ill-structured, that it could not be salvaged
but needed a complete rewrite. And they didn't have the budget to do that. So the app
still hangs, still crashes, and still loses packets.
                                        joe

On Sun, 30 May 2004 10:49:52 -0400, A. J. Rappaport
<please.reply.in.newsgroup.only@spamsucks.com> wrote:

>On Sat, 29 May 2004 22:16:16 -0600, Jerry Coffin <jcoffin@taeus.us>
>wrote:
>
>> A spinlock is suitable only what you're reasonably certain you'll
>> only block for a _very_ short period of time. The problem is pretty
>> simple: it (normally) uses as much CPU time as it can while it's
>> waiting. This is in direct contrast to waiting on a mutex, event,
>> etc., which block the thread so it uses essentially no CPU time until
>> the condition on which you're waiting is satisfied.
>
>Good point.
>
>Actually, what I'm doing is ...
>
>if ( !flag )
>{
> Sleep( 1 ); // or Sleep( 0 ) to just give up
> // the rest of the timeslice, which is
> // pretty much all that Sleep( 1 ) does
> // anyway...
>}
>
>... so the CPU loading isn't so much, but I see your point.
>
>Thanks...

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm



Relevant Pages

  • Re: Why RosAsm Breaks on a large number of symbols
    ... Polling is a process of waiting for and event to ... In this context, a Windows message loop is no different, it is a loop ... that calls the GetMessageAPI, waits for it to return and responds ... harping on about how an alternative design must be used is the method ...
    (alt.lang.asm)
  • 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: 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)