while true loop, would a semaphore work?

  • Thread starter Thread starter Logan McKinley
  • Start date Start date
L

Logan McKinley

I am trying to create a high precision timer using the
QueryPerformanceCounter API call which works find but I want to implement a
tick event and am not sure how best to proceed.
Say the user wanted the tick event every 500 ms, the only way I can think of
to perform that is constantly querying QueryPerformanceCounter until 500 ms
has passed then raise the event but that will use 100% of the processor,
which I don't want to do. Is there any way to implement this timer without
using 100% of the processor or is there a different way to go about this I
have not thought of?
Thanks in Advance,
~Logan
 
What is your tolerance for error? If you wantr high-precision timing .net
may be the wrong thing to use. If your requirements are high-enough,
Windows might be the wrong platform.

You could get a timer PCI card to get better results, but that's getting
into the extreme.

It all depends on what you mean by "high-precision."
 
Logan McKinley said:
I am trying to create a high precision timer using the
QueryPerformanceCounter API call which works find but I want to implement a
tick event and am not sure how best to proceed.
Say the user wanted the tick event every 500 ms, the only way I can think of
to perform that is constantly querying QueryPerformanceCounter until 500 ms
has passed then raise the event but that will use 100% of the processor,
which I don't want to do. Is there any way to implement this timer without
using 100% of the processor or is there a different way to go about this I
have not thought of?

How accurate do you need it to be? Thread.Sleep won't be *terribly*
accurate, but it might be good enough.

Alternatively, you could have a sort of hybrid system which says: "As
long as I'm more than 100ms away from the due time, I'll call sleep to
try to get me to 100ms away from the due time, and after that I'll
loop". (Where 100ms is a figure plucked out of thin air :)
 
Create a schedule/worker thread that just loops and calls delegates and/or
signals your mutex, event or sem.

while(true)
{
if ( threadStop )
break;
if ( needToDoStuff ) //maybe some bool that is set outside your
thread - need to sync
DoSomeStuff(); //Call delegates, signal sem, etc.
Thread.Sleep(500);
}
 
Jon Skeet said:
How accurate do you need it to be? Thread.Sleep won't be *terribly*
accurate, but it might be good enough.

Actually thread.sleep would be quite INaccurate because it tells the Windows
sheduler to also relinquish the threads time. Of course no thread can run for
500 ms without being preempted anyways, but you see the point.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
Chad Z. Hower aka Kudzu said:
Actually thread.sleep would be quite INaccurate because it tells the Windows
sheduler to also relinquish the threads time. Of course no thread can run for
500 ms without being preempted anyways, but you see the point.

Absolutely - which is why I suggested that if accuracy is an issue, you
ask to sleep until some margin before the due time, so that you should
wake up some time before the due time.

If you don't want the thread to relinquish any time, you might as well
just use the tight loop, but then you might get interrupted anyway...
 
Back
Top