Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » running program at different speeds
running program at different speeds [message #37007] Wed, 08 August 2012 08:53 Go to next message
varu is currently offline  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 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 #37009 is a reply to message #37007] Wed, 08 August 2012 11:29 Go to previous messageGo to next message
jerson is currently offline  jerson
Messages: 202
Registered: June 2010
Location: Bombay, India
Experienced Member

Another possible way would be to set up a "timer callback" to execute a code fragment every timer interval.
Re: running program at different speeds [message #37012 is a reply to message #37008] Wed, 08 August 2012 13:00 Go to previous messageGo to next message
varu is currently offline  varu
Messages: 21
Registered: March 2012
Location: Bangalore,India
Promising Member
Dear Honza,

its working for me thanks..but it is creating 10 ms delay for sleep(1).i did not understand why this happening this and using Getickcount() for finding time taken by my loop..can please explain

thank you
varun
Re: running program at different speeds [message #37013 is a reply to message #37012] Wed, 08 August 2012 13:55 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 #37015 is a reply to message #37013] Thu, 09 August 2012 06:11 Go to previous messageGo to next message
varu is currently offline  varu
Messages: 21
Registered: March 2012
Location: Bangalore,India
Promising Member
Hello Honza

Thanks for explanation...i am using ubuntu 64 bit...and one more observation which i made is that delay time is toggling between 9ms,10ms alternatively can u please tell me hw will u justify this..

thanks
varun
Re: running program at different speeds [message #37016 is a reply to message #37015] Thu, 09 August 2012 07:13 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 #37018 is a reply to message #37016] Thu, 09 August 2012 08:27 Go to previous messageGo to next message
varu is currently offline  varu
Messages: 21
Registered: March 2012
Location: Bangalore,India
Promising Member
yes..the main reason for this is that suppose i have defined some set of activities to done in every 100ms and out of that one of the activity takes 10ms of out of 100ms..nw if this 10ms activity takes more than 10 ms then all activities defined in 100ms activity may not happen in the same cycle instead it will happen in next cycle which will case one cycle delay..and for 4 or 5 hrs activity the error is accumulated so..

thanks
varun
Re: running program at different speeds [message #37020 is a reply to message #37009] Thu, 09 August 2012 11:20 Go to previous messageGo to next message
varu is currently offline  varu
Messages: 21
Registered: March 2012
Location: Bangalore,India
Promising Member
Hello Jerson

is it possible to use timer call back in core console app projects also?

thanks


Best regards
varun
Re: running program at different speeds [message #37021 is a reply to message #37020] Thu, 09 August 2012 12:20 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 #37022 is a reply to message #37021] Thu, 09 August 2012 12:48 Go to previous messageGo to next message
varu is currently offline  varu
Messages: 21
Registered: March 2012
Location: Bangalore,India
Promising Member
Hello Honza

how to use set time call back in core conole app.
i tried using it but got the following error
no matching function for call to ‘Upp::Callback::Callback(void (&)())’  

 candidates are:  Upp::Callback::Callback(Upp::_CNULL)
                 Upp::Callback::Callback()  Upp::Callback::Callback(Upp::CallbackAction*)                   Upp::Callback::Callback(const Upp::Callback&)  




Re: running program at different speeds [message #37030 is a reply to message #37022] Fri, 10 August 2012 08:01 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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
Re: running program at different speeds [message #37045 is a reply to message #37030] Sat, 11 August 2012 08:31 Go to previous messageGo to next message
varu is currently offline  varu
Messages: 21
Registered: March 2012
Location: Bangalore,India
Promising Member
Hi Honza,

can u please tell me what do mean by "must be compiled by MT Flag"..and how to do it..

Thanks


varun
Re: running program at different speeds [message #37047 is a reply to message #37007] Sat, 11 August 2012 10:28 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi Varun,

The MT flag tells U++ to add support for multithreading. To learn how to add flag to main package see Getting started with TheIDE paragraph 3.1. The list of standard flags and their meanings can be found on this page.

Honza
Re: running program at different speeds [message #37050 is a reply to message #37047] Sat, 11 August 2012 13:59 Go to previous messageGo to next message
varu is currently offline  varu
Messages: 21
Registered: March 2012
Location: Bangalore,India
Promising Member
hi honza

Thanks..my program is error free..and i am using

the timer as shown by you

ie

Timer t;
t.SetTimeCallback(-500,callback(SomeFn));

but this "SomeFn" does not get called at all...can u please tell me why


thanks

varun
Re: running program at different speeds [message #37051 is a reply to message #37050] Sat, 11 August 2012 14:43 Go to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hard to tell without seeing more of the code... The only thing coming to my mind is that your program exits before the timer has even chance to call the callback.

Honza
Previous Topic: Add layout
Next Topic: Trouble with Socket
Goto Forum:
  


Current Time: Sat Apr 20 09:13:25 CEST 2024

Total time taken to generate the page: 0.04629 seconds