Re: "while loop" in a thread
runcyclexcski@yahoo.com wrote:
"continuously" in my case means a frame delivered every 300
milliseconds. I may need to go to 30 millseconds down the road. The
while loop checks if a new frame is available (using function that is
in a OEM library that came with the camera), and when a new frame is
available, the program displays it.
Even if I don't display any frames (essentially, a blank while loop,
for testing purposes) I still get the freeze.
What matters here is what happens when you check and a new frame is not
available. If you don't have any way to suspend the thread at that
point then you are asking the processor to spend all its time rechecking.
You can insert a Sleep() call in the animation loop to use less CPU time.
If Sleep() is precise enough so that I don't miss a frame, OK.
Sleep() will generally give you a resolution of 10 msec. But neither
polling continuously nor Sleep() is a good design.
You can make the animation event-driven instead of an unbounded loop.
When the incoming data completes a frame call the window Invalidate,
then return to the message pump.
See, I use events elswhere in my app, but I don't understand the
"phylosophy" behind events. Does't the event triggering istelf imply a
while loop somewhere in the system waiting for the event to happen?
No. Any while loop, anywhere, will attempt to burn up all available CPU
time. Waiting for an event is much more efficient. It works
approximately like a 10 msec interrupt: At each interrupt the OS checks
for pending events and decides which thread to execute next.
--
Scott McPhillips [VC++ MVP]