Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WaitForSingleObject() Accuracy

Status
Not open for further replies.

mattKnight

Programmer
May 10, 2002
6,238
GB
Hi all,

another newbie question I'm afraid to do with multi-threading!

I am writing a scheduler routine
brief outline is

1) Read value from registry for activation time
2) compare to current system time & calculate number of seconds until time to activate.
3) enforce a "wait" period on current thread for that time period

steps 1 & 2 are implemented and working ok! (wonder willnever cease [bigsmile])
howvever to the wait state...
suppose the delay is 3600 secs (1 hour)
Is it better to use
Code:
WaitForSingleObject(hEvent, 3600*1000);
or
Code:
for (int nCount = 0; nCount >3600;nCount++)
{
WaitForSingleObject(hEvent, 1000);
}
hEvent will never get signalled btw

As I see it, the first is likely to be less accurate due to vaugaries of WINNT timing. Is that correct? Will the timing also be dependant on CPU stress?

Using the second outline, delay can be re-evaluated and timing inaccuracies eliminated.

The timings do not [bold]really[/b] have to be precise - but pride dictates that it should be close

Is there another (better) way of wasting time?

Thanks

Matt
 
I would loop around a Sleep(1000) and after each sleep check () check if it is time to do the job - simply by reading the clock.

Typically you want to be able to terminate the loop (e.g. a service which the user want's to stop). This can be checked while you check the time.

/JOlesen
 
MattKnight,

You made a post in my thread "Adding to Windows Context Menus"..

The thread link you gave me lead me to the wrong thread? What is the proper thread?

Tony

(I noticed you hadn't marked my thread for email notification...) Did I help?
Vote!
 
You are better to use the for loop. Don't use the sleep function, WaitForSingleObject is much nicer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top