Timer Queues

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Timer Queues

Post by Rescator »

http://msdn.microsoft.com/en-us/library ... 85%29.aspx
CreateTimerQueueTimer() and related are supported on W2K+.
Does something similar exist on Linux and Mac? If so maybe a native PureBasic Timer Queue that basically wraps the API's on these platforms would be a nice feature?

I certainly would love that instead of having to roll my own. Obviously the issue here is Win9x which probably would need some extra code to emulate such a queue though.
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Timer Queues

Post by STARGÅTE »

sounds like my include:
Include - Trigger , signal forms with timer and more

EDIT: Or you can write it your self with Threads
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Timer Queues

Post by freak »

What does this do that you cannot do with AddWindowTimer() ? (except that it doesn't use a window, but threads instead)
quidquid Latine dictum sit altum videtur
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Timer Queues

Post by Rescator »

The timer queues (on windows at least) will change the OS timer period/precision automatically in needed. (they basically do something similar to timeBeginPeriod_() internally)
so you get down to ms precision, I believe window timers just use whatever timer precision the system happens to be in, and there is also the messaging overhead (vs this queue system).

This also takes advantage of the fact that the timer is run in a timer thread (or a new queue thread if you create that) so it takes advantage of threading/cores much better, I also believes the OS set a "timer" priority on the queue thread(s) as well, OS timer thread pools or whatever MS calls them.

I mean it's not like I can't do the API stuff myself, but I thought if Linux and Mac has similar then it might be worth looking into a bit more? as it would be solid crossplatform timer queue in that case.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Timer Queues

Post by netmaestro »

What does this do that you cannot do with AddWindowTimer() ?
CreateTimerQueueTimer_() (which PureBasic currently doesn't import btw) allows you to reduce timer intervals to a true 1ms, which is necessary for smooth animations. That would be the main one for me.
BERESHEIT
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Timer Queues

Post by freak »

netmaestro wrote:
What does this do that you cannot do with AddWindowTimer() ?
CreateTimerQueueTimer_() (which PureBasic currently doesn't import btw) allows you to reduce timer intervals to a true 1ms, which is necessary for smooth animations. That would be the main one for me.
But it does so using threads, which means you can't use DirectX in them (so no Sprite, Screen, etc). What is the use then for animation?
quidquid Latine dictum sit altum videtur
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Timer Queues

Post by Rescator »

Hmm, darnit. You're right...

Hey freak I found this http://www.virtualdub.org/blog/pivot/entry.php?id=272
It's an interesting read. (the vdub dev really knows about timing APIs etc.)
He has a solution mentioned a bit later in the article.
I'm also looking into another API he's mentioned.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Timer Queues

Post by netmaestro »

Problem is, the guy is wrong. timeGetTime_() is not 1ms accuracy and so his whole premise is thrown off. Consider this test:

Code: Select all

Import ""
  CreateTimerQueue()
  CreateTimerQueueTimer(a,b,c,d,e,f,g)
EndImport

Global cc

Procedure Timer(parameter.i, b.b)
  cc+1
EndProcedure

tq = createtimerqueue()

CreateTimerQueueTimer(@thandle.i, tq, @Timer(), 0, 0, 1, $80)

Delay(10000)
Debug cc
On my machine cc is reported as 10000 exactly after running 10 seconds. The accuracy is quite adequate despite what the fellow posits.

@freak: I wasn't even considering DX when I made the comment, there are lots of applications for smooth animation in gui's. An RSS feed scrolling across the bottom of your window, possibly tips of the day same location, and an aboutbox with a nice smooth scrolling presentation is very attractive. Custom wait/transfer animations, etc. With standard timing these all look choppy. These kinds of effects appeal to me as a gui programmer but I understand perfectly if others don't see the point. :mrgreen:
BERESHEIT
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Timer Queues

Post by Rescator »

Actually timeGetTime_() has 1ms precision, but it can only ever get close to that using timeBeginPeriod_() and even if set to 1ms you are not guaranteed constant 1ms timing, it may fluctuate from 0ms to 2ms.

And if I recall correctly, timeBeginPeriod_() will also affect Sleep_() and therefore also PureBasic's Delay().

@netmaestro
the vdub guy needed way more precision than 10 sec. He needed 1/60th or better.
He's also written about vsync/vblank/scanlines and more previously in his hunt for stable frame playback and syncing.

PS! Your browser (or certain plugins), your media player, etc. tend to mess with timeBeginPeriod_()
For example, playing a track in foobar2000 makes foobar2000 change timeBeginPeriod_() to 1ms, even when paused, only when stopped does it use timeEndPeriod_() and return system timings to normal. Windows Media Player uses 5ms.
So testing timings can be a pain if you forget, vdubs's dev even mentioned his media player messing up the testing, heh...
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Timer Queues

Post by netmaestro »

the vdub guy needed way more precision than 10 sec. He needed 1/60th or better
I don't think you understand my test... 10000 procedure calls in 10 seconds shows true 1ms granularity. Much better than 1/60.
BERESHEIT
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Timer Queues

Post by c4s »

netmaestro wrote:
the vdub guy needed way more precision than 10 sec. He needed 1/60th or better
I don't think you understand my test... 10000 procedure calls in 10 seconds shows true 1ms granularity. Much better than 1/60.
Btw: Here it's about 5100.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Timer Queues

Post by Rescator »

Doh! You are right. That's 1/1000 actually.

Maybe he didn't realize that the timer queues are also affected by timeBeginPeriod_() ? (I think they are, although the MSDN article doesn't seem to mention that?)
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Timer Queues

Post by Demivec »

c4s wrote:
netmaestro wrote:
the vdub guy needed way more precision than 10 sec. He needed 1/60th or better
I don't think you understand my test... 10000 procedure calls in 10 seconds shows true 1ms granularity. Much better than 1/60.
Btw: Here it's about 5100.
I also get about 4960 (whether using debugging output or MessageRequesters). Using Win XP SP3 (x86)
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Timer Queues

Post by freak »

Just a question: what do you need 1ms granularity for animations, when the human eye cannot see more than 25 updates per second anyway? (which would be 40ms each)

I am reluctant to implement something as simple as a timer through threads, because threads make everything magnitudes more complicated: You have synchronization issues, race conditions, deadlocks and a whole host of other non-deterministic behavior which make it very hard to write correct programs. Thread programming done right in a language like PB is hard, so requiring that somebody learn all this stuff just to get a timer working is ridiculous. (plus, there is the whole slowdown of compiling the program in thread safe mode)

Btw, i think smooth animations in GUI programs do not require threads. This is possible even in a single thread given that GUI programs do nothing but wait for input 99% of the time anyway.
quidquid Latine dictum sit altum videtur
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Re: Timer Queues

Post by dhouston »

I don't do animation so I cannot comment on that aspect but Windows does have high precision timers capable of sub-µSec resolution. I do have uses for high precision timers. For example, you can capture signals from IR and RF remotes via LineIn if you have a high resolution timer.

For Linux, see...I don't know about OS-X.

EDIT: It appears there is a way to get 1 µS resolution under Linux, Unix & OS-X...
Post Reply