?
Solved

worker thread

Posted on 2002-04-19
7
Medium Priority
?
338 Views
Last Modified: 2012-05-04
I have created a worker thread that "polls" a hardware device to determine if some data are ready. This worker thread is created with the lowest priority possible. When this worker thread is running (and it may be running all the time) the CPU cycles on the performance tab of the windows NT task manager is pegged at 100%.

I can't use a timer because the hardware can give data anywhere from .02 seconds to hours.

Is their another approach I can take or create the worker thead in a different manner that will not steal all the CPU cycles?

0
Comment
Question by:johndixson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 600 total points
ID: 6954660
Polling is NEVER a good idea.  

But in this case it's not an issue since ANY thread with a higher priority (which should be EVERYTHING EXCEPT IDLE) will run before this polling thread runs.  But if you use this technique, get used to seeing CPU time pegged at 100%.  The system is doing EXACTLY what you have programmed it to do, namely run your polling thread when nothing else wants to run.

The correct approach to this is to use an interrupt to signal your device driver that data is ready.  This can flag your thread through an event or whatever.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6954673
Even though I completly agree with jhance about "polling", a first step to reduce the CPU usage might be to simply

for ( ;;) { // "polling loop"

 Sleep ( 20); // .02s
}
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6955767
"When this worker thread is running (and
it may be running all the time) the CPU cycles on the performance tab of the windows NT task manager
is pegged at 100%. "

Why does that bother you?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:job_s
ID: 6955809
Use any blocking method on the thread function for avoiding the infinite loop working all time.


0
 
LVL 3

Expert Comment

by:stefanr
ID: 6957702
Or, if possible, use an OVERLAPPED structure to read the data anynchronously... What kind of device is it you are reading from? Is it opened with CreateFile?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6958354
>> can't use a timer because the hardware can give data anywhere from .02 seconds to hours.

That constraint does not preclude the use of a timer.  Just set the timer to occur at regular intervals and poll once, then return.  If it is important that you get the data right away (e.g., another chunk of data may come right on its heels and you can't risk losing the previous data), then the intervals need to be 20ms apart.

-- Dan
0
 

Author Comment

by:johndixson
ID: 6971706
After you suggestion I did some research into the hardware and it turns out it does support triggering a Win32 event when data is ready. It wasn't in the documentation I had on the device it was in a readme file on the disk.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question