|
|
Home » Community » Newbie corner » running program at different speeds
running program at different speeds [message #37007] |
Wed, 08 August 2012 08:53  |
varu
Messages: 21 Registered: March 2012 Location: Bangalore,India
|
Promising Member |
|
|
Dear Sir,
Is there any way to run my loop in my program at different time period in core console app..for example i want to run my program every 10ms only instead of the speed at which is running now..
thanks
varun
|
|
|
Re: running program at different speeds [message #37008 is a reply to message #37007] |
Wed, 08 August 2012 09:54   |
|
varu wrote on Wed, 08 August 2012 08:53 | Dear Sir,
Is there any way to run my loop in my program at different time period in core console app..for example i want to run my program every 10ms only instead of the speed at which is running now..
thanks
varun
|
Hi Varun,
I'm not sure if I understand correctly, but to slow down any loop, you can just insert call to Sleep(int ms) in it. E.g.:while(work_not_done){
//do something
Sleep(10); // wait ~10 ms before continuing
}
Sleep is defined in Core package, so it can be used in any application. If you are using Threads, there is also similar function Thread::Sleep().
Best regards,
Honza
|
|
|
|
|
Re: running program at different speeds [message #37013 is a reply to message #37012] |
Wed, 08 August 2012 13:55   |
|
On POSIX platforms Sleep() is implemented using nanosleep function. If you look in manual for this function it says this:
man nanosleep | The suspension time may be longer than requested because the argument value is rounded up to an integer multiple of the sleep resolution or because of the scheduling of other activity by the system.
|
Very similar conditions hold AFAIK for the windows implementation.
In addition (at least on POSIX) it is possible that Sleep returns earlier then it is supposed, if it is interrupted by a signal from outside.
If you need to get more precise speed adjustments, you might want to try longer Sleep() less often (e.g. every n-th iteration of the loop).
Honza
|
|
|
|
Re: running program at different speeds [message #37016 is a reply to message #37015] |
Thu, 09 August 2012 07:13   |
|
There are two factors in this: One is that when you call sleep function, the kernel switches context and lets another process to run. That might make the sleep longer, as mentioned in my previous post. The second factor you have to take in account is the precision of the mechanism you use to measure the time elapsed. If you search the forum, you will find couple threads about precise time measurements and what to be aware of.
Is there any particular reason why you need every interval to be *exactly* the same?
Honza
|
|
|
|
|
Re: running program at different speeds [message #37021 is a reply to message #37020] |
Thu, 09 August 2012 12:20   |
|
varu wrote on Thu, 09 August 2012 11:20 | Hello Jerson
is it possible to use timer call back in core console app projects also?
thanks
Best regards
varun
|
Yes, there is a Timer package in bazaar, that allows using timer in console apps. However, I'm not sure if it is going to match your precision requirements...
Honza
|
|
|
|
Re: running program at different speeds [message #37030 is a reply to message #37022] |
Fri, 10 August 2012 08:01   |
|
The simplest possible example is something like this:Quote: | #include <Core/Core.h>
using namespace Upp;
//must be compiled with MT flag
#include <Timer/Timer.h>
void SomeFn(){
Cout() << GetSysTime() << "\n";
}
CONSOLE_APP_MAIN{
// set up the periodic callback
Timer t;
t.SetTimeCallback(-500,callback(SomeFn));
// wait until the work is done
Sleep(5000);
}
|
The error you got was about passing wrong callback or something similar. Have a look at callbacks in U++ manual, they is many ways to work with them.
Honza
|
|
|
|
|
|
|
Goto Forum:
Current Time: Sun Apr 27 00:23:18 CEST 2025
Total time taken to generate the page: 0.00498 seconds
|
|
|