Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

worker thread

Posted on 2002-04-19
7
Medium Priority
?
341 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Technology Partners: 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

610 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