Solved

worker thread

Posted on 2002-04-19
7
330 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
7 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
"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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

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


0
 
LVL 3

Expert Comment

by:stefanr
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now