Real Time-ing

BBaz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 8 15:59:17 PST 2015


On Tuesday, 8 December 2015 at 16:40:04 UTC, Taylor Hillegeist 
wrote:
> On Tuesday, 8 December 2015 at 15:35:18 UTC, Taylor Hillegeist 
> wrote:
>> So, I mostly do programming that is of run to completion
>
> I took a stab at the problem:
>
> http://dpaste.dzfl.pl/2eef530d00fc
>
>
> 0 nsecs with jitter of :500000000 nsecs
> 498937256 nsecs with jitter of :1062744 nsecs
> 499036173 nsecs with jitter of :963827 nsecs
> 500025650 nsecs with jitter of :25650 nsecs
> 500020735 nsecs with jitter of :20735 nsecs
> 499057062 nsecs with jitter of :942938 nsecs
> 498932033 nsecs with jitter of :1067967 nsecs
> 591037317 nsecs with jitter of :91037317 nsecs
> 499032794 nsecs with jitter of :967206 nsecs
> 499034637 nsecs with jitter of :965363 nsecs
> 499022963 nsecs with jitter of :977037 nsecs
> 498976577 nsecs with jitter of :1023423 nsecs
> 499076723 nsecs with jitter of :923277 nsecs
> 499023885 nsecs with jitter of :976115 nsecs
> 499018048 nsecs with jitter of :981952 nsecs
> 499004224 nsecs with jitter of :995776 nsecs
> 499048461 nsecs with jitter of :951539 nsecs
> 499013747 nsecs with jitter of :986253 nsecs
> 499018048 nsecs with jitter of :981952 nsecs
> 499007604 nsecs with jitter of :992396 nsecs
>
> However i seem to get jitter of around 1 ms. Is there anything 
> else i can do to improve?

1) a computer is not real-time, for example audio is always 
buffered (e.g 512 samples)

2) thread are not good for timing. In a simple test you could get 
satisfying results while in a stressed IRL environment it won't 
work as well. so you should rather use the OS API to make a timer 
(or the one proposed by a framework if it applies, for example 
gui app with SDL: SDL timer).

However if you want to have better times you can use core.time in 
a thread callback:

~~~~~~~~~~~~~~~~~
// to, t1 and interval are uint;
if (!t0) t0 = TickDuration.currSystemTick.msecs;
t1 = TickDuration.currSystemTick.msecs;

if ((t1 - t0) > interval)
{
     t0 = 0;
     onTimer();
}
~~~~~~~~~~~~~~~~~


More information about the Digitalmars-d-learn mailing list