Solved

worker thread

Posted on 2002-04-19
7
332 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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