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
Solved

worker thread

Posted on 2002-04-19
7
333 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 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