![]() |
passive form pof waiting
Hello,
I'm adding the finishing touches to my program, and I'd like you to help me with this, I haven't found any way to do it. So, I want the program to execute a series of instructions after every x seconds. Now, I have to find a form of passive waiting during which the program "doesn't do anything", but doesn't use up any or almost no CPU. I've tried it with the clock_t class and various other methods but I always end up with 50 percent of CPU power being used while waiting. I know it's logical, but can someone find a way to do this without using that much CPU? Thank you, Leslie EDIT : sorry for the title:) |
id also be interested in hearing any solutions to this as well!
|
There's no standard way in C/C++ to do this.
However, virtually every modern operating system support means to do this -- although the method is operating system dependent. For example, if you look in the win32 API there is a function named Sleep() that can be used to sleep for (approximately) a specified number of milliseconds and a function named SetTimer() which allows a function to be called periodically. |
Thank you for the reply. The Sleep() method still uses 50 percent CPU, however I don't know how to use the SetTimer one. Could you give an example? I was also wondering if there is a way of doing this with interrupt handling. It could be much more effective... I recently read about int 8h, does anyone know if it can help us?
Thank you, Leslie |
You'll really need to provide more info. SetTimer is a rather simple Win32 API function, but if you're not familiar with the API backbone of Window Procedures and Message then it's not going to be much use on it's own. Is the program Console based or GUI?
As for Sleep, that should, dare I say will, work. Just call it when you want the program to do nothing specifying how long you want it to do nothing for. If you're in Windows then interrupts are not the way to go. |
One way that works well in console-mode programs (ie, is not dependant on the Windows message pump) is timeSetEvent(). You will need to include the <mmsystem.h> header, as well as link in winmm.lib (or libwinmm.a, if you're using DevC++).
Using this approach, you can set up a callback function to be invoked every x milliseconds, which is essentially what you want to do. Just don't shove too much code into the callback function; remember that since it effectively operates in an interrupt context, you need to keep it short. Don't forget to call timeKillEvent() when you're done, either. Should you need to do something more complicated, you'll probably want to look into making your program multithreaded, and having a worker thread that periodically does a task, and then sleeps. By using API functions to get the elapsed time it takes for this thread to do its task once, you can calculate how long you'll eed to sleep for. In other words, if you want to do the task once every five seconds, but it takes 2 seconds to do it, you want to sleep for 3, not 5. As a sleeping thread consumes no CPU time (the task scheduler won't give it the CPU), it will use minimal CPU resources. |
Quote:
|
To keep it short, it is a managed .NET application. I want it to execute a quite long and complicated code every x seconds. I tried the sleep function, but for some reason the CPU was still working 50 percent.
|
Well if you're going to keep it that short there is very little anyone can do to help. To put it bluntly Sleep() will work, if it isn't then you are doing something wrong. Put the code to be executed inside a loop along with the Sleep instruction set to X seconds.
Unless your app is multi-threaded of course, because Sleep only 'sleeps' one thread so if a second thread is still working away then the cpu is still in use. P.S. I'm gonna guess you've a dual core so that 50% is really 100% of one core. |
Quote:
|
| All times are GMT -5. The time now is 2:43 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC