Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   passive form pof waiting (http://www.programmingforums.org/showthread.php?t=13437)

Lesliect6 Jun 28th, 2007 8:51 AM

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:)

rwm Jun 28th, 2007 9:14 AM

id also be interested in hearing any solutions to this as well!

grumpy Jun 28th, 2007 10:14 AM

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.

Lesliect6 Jun 28th, 2007 11:12 AM

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

Eoin Jun 28th, 2007 2:58 PM

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.

lectricpharaoh Jun 28th, 2007 5:42 PM

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.

Dameon Jun 28th, 2007 6:45 PM

Quote:

Originally Posted by Lesliect6 (Post 129772)
The Sleep() method still uses 50 percent CPU

This statement alarms me.

Lesliect6 Jun 28th, 2007 6:47 PM

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.

Eoin Jun 28th, 2007 7:03 PM

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.

grumpy Jun 28th, 2007 7:14 PM

Quote:

Originally Posted by Lesliect6 (Post 129784)
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.

Sleep() does not behave like this. It probably means that your "long and complicated" code is using about half the CPU time (making an application sleep between calls of a function does not magically make that function consume no CPU time), or you have another application or thread running that is consuming CPU time [(Sleep() works by yielding your thread's time-slice, and allowing another thread to execute].


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