By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

Strange thread sleep vs. CPU usage phenomenon

P: n/a
I have a thread which is used for updating a display window. The
normal paint message queue is being bypassed in favor of drawing on
demand by this thread. This thread is passed a Graphics context,
draws it's image, and then sleeps for a small number of milliseconds.

I'm on windows 2000 and watching the CPU usage graph when I run my
program. For the most part, CPU usage corelates to the length of the
sleep delay in my thread. The longer the sleep delay, the smaller the
CPU usage. The usage also stays pretty constant, not varying by much
more than +-4%.

However, when I set a sleep time of 10 milliseconds precicely, my CPU
usage fluctuates all over the place. Some cycles I get 2% usage, and
others I'm up at 97%. I'm not sure what's so magic about the number
10. Is there anyone that could provide insight?

Mark McKay
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On the wall of the virtual bathroom stall, ma**@kitfox.com (Mark McKay) scratches:
I have a thread which is used for updating a display window. The
normal paint message queue is being bypassed in favor of drawing on
demand by this thread. This thread is passed a Graphics context,
draws it's image, and then sleeps for a small number of milliseconds.

I'm on windows 2000 and watching the CPU usage graph when I run my
program. For the most part, CPU usage corelates to the length of the
sleep delay in my thread. The longer the sleep delay, the smaller the
CPU usage. The usage also stays pretty constant, not varying by much
more than +-4%.

However, when I set a sleep time of 10 milliseconds precicely, my CPU
usage fluctuates all over the place. Some cycles I get 2% usage, and
others I'm up at 97%. I'm not sure what's so magic about the number
10. Is there anyone that could provide insight?


Perhaps when you wakeup on the 10ms interval you are competing for resources
with some other system thread(s).

Jul 17 '05 #2

P: n/a
> However, when I set a sleep time of 10 milliseconds precicely, my CPU
usage fluctuates all over the place. Some cycles I get 2% usage, and
others I'm up at 97%. I'm not sure what's so magic about the number
10. Is there anyone that could provide insight?


CPU usage calculations and similar are often implemented by 'sampling'
values. In your case it probably samples every 10 ms. This means that every
10 ms it checks which thread is running and gives it credit for the whole
period.

Niels Dybdahl
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.