Go to Sleep
- From: "randyhyde@xxxxxxxxxxxxx" <randyhyde@xxxxxxxxxxxxx>
- Date: 23 Aug 2005 17:00:01 -0700
hutch-- wrote:
>
> Show us how you provide an alternative to a polling loop that checks an
> exit code, polls a keystroke and displays duration on te console title
^^^^^^^^^^^^^^^^^
Well, clearly you can't provide an alterative to polling that does
polling. Then it wouldn't be an alternative to polling, would it :-)
> bar while running at ZERO percent processor usage. How many percentage
> points improvement can you get with the technique you have insisted is
> technically superior ?
I have no idea what the argument is about, nor do I really care. But as
long as the OS can provide an event that you can wait on, waiting for
the object is generally better than polling until the object is
available. And this really has little to do with efficiency and
*everything* to do with proper synchronization. You can wait for a
process to complete (so you can check its error code return value), you
can wait for a keystroke to occur. I'm not sure what polling has to do
with displaying a duration in console title bar, so I can't comment on
that. Bottom line, though, is that sleeping and periodically waking up
(to poll for some event) is the right thing to do *only* when there is
no way for the hardware (e.g., keyboard) or system (e.g., a process
terminating) to notify your process that an event has occurred.
Whether or not sleep is significantly slower, this just *isn't* the
right thing to do. The right thing to do is to wait on the specific
events (process termination, to get the return code, or a keypress
event). Even if these operations were *less* efficient (and they are
not), blocking on those events is the right way to handle them. Sleep
is a hack, pure and simple.
Let me share a story with you. I'm currently rewriting a control
program for a small research reactor. The software is written in C
under QNX 4.0. The guy who originally ported the code from ICDOS to QNX
4.0 was fond of sticking in "delay(n);" calls all over the place (like
sleep, but in milliseconds). There are probably a dozen different
processes running on two computers (one in the control room, one down
by the reactor). All synchronization is done by these delay calls. As a
result, when faster computers come out, the synchronization between the
two systems and all the processes fails -- it's not unlike the games of
yesteryear that used a delay loop for timing (which broke
spectactularly when faster CPUs arrived). Debugging this code is a PITA
because failures aren't consistent. All of these could have been
avoided if proper synchronization primitives had been used.
Maybe your little sample program (whatever it is) works just fine with
a sleep loop. But by using sleep, and not learning how to do it
properly, chances are pretty good that you'll use sleep again, the next
time you need a synchronization primitive. And the time after that. And
the time after that. And before you know it, you've got a very unstable
system controlling a nuclear reactor. Better to figure out how to do it
right from day one and practice that methodology in all code you write.
That way, you don't wind up with fragile code because you've gotten in
the habit of using a hack to synchronize things.
Again, I want to point out that I've not got a clue what your program
does. But unless it's measuring time intervals or waiting on some
time-based event, sleep is not the proper primitive to use here.
Cheers,
Randy Hyde
.
- Follow-Ups:
- Re: Go to Sleep
- From: f0dder
- Re: Go to Sleep
- References:
- Re: The Donkey Challenge
- From: Donkey
- Re: The Donkey Challenge
- From: hutch--
- Re: The Donkey Challenge
- From: Donkey
- Re: The Donkey Challenge
- From: hutch--
- Re: The Donkey Challenge
- Prev by Date: Re: The revelation of St. f0dder the Divine
- Next by Date: Re: The Betov Digest
- Previous by thread: Re: The Donkey Challenge
- Next by thread: Re: Go to Sleep
- Index(es):
Relevant Pages
|