[4.2] Windows multitasking CPU usage
Gerwin Broers

I have two programs made with Allegro v4.2 (windows).
When running these programs windowed, then switching out and load up another application to make a screenshot or something else: the system gets really slow. The system I tested this on is Windows 2000 SP5 on a single core Athlon 3000+. The programs are compiled with both MSVC6 and Dev-C.

For the second program, which is a development tool for a game, it is important to me that users can multitask it comfortably. The programs are mostly in a loop just polling the keyboard.

I read about a CPU idle patch for allegro 4.4 in this forum, but did not find any reference to it in the latest allegro 4 changelog, so did not try v4.4 yet.

There are task switch in and switch out hook functions, but since I don't know how to idle, what should I make the program do at task switch out?

Audric

If your program currently takes 100% (of one) CPU at all times, it lacks
rest(number_milliseconds)

Gerwin Broers

wow, I just added "else rest(10);" at the end of the key polling and all multitasking troubles have vanished, while the program itself seems as snappy as before.

I should have learned about this years ago.

Thanks a lot Audric.

Thomas Fjellstrom

resting is not the best option. Look at the "Making use of the ticks" section of the Wiki's Timers Article.

Audric

That section uses rest() too, were you thinking of the semaphore technique ? It saves less CPU, it would gain timing precision in theory (*) but I think the game is his "HeroQuest", a turn-based board game with little animation except the mouse cursor... The difference would be invisible.

When it comes to CPU saving / optimization, my guidelines are
Priority 1) be sure to rest(>0) when "nothing to do"
Priority 2) don't redraw the screen if nothing has changed
Priority 3) ...don't bother anymore :)

(*) not on Windows because of its 10ms timer granularity

Edgar Reynaldo
Audric said:

(*) not on Windows because of its 10ms timer granularity

I think this is a badly perpetuated myth - I've never seen rest take more than an additional millisecond or so of overhead to return. (That's on Vista, and maybe XP as well).

Elias

I guess maybe Windows 98 and also Linux systems from that time (it's a kernel setting there I think) could use 10ms. I have never seen it either though.

[edit:] rest(10) instead of rest(1) will use 10ms of course.

Gerwin Broers

This question was for TFDtool:
http://combatace.com/topic/68667-alternate-tfd-terrain-tilemap-editorviewer/
But I will also put it in heroquest when I find the time.

I adjusted it to:
if (keypressed())
{
//key polling
}
else rest(5);

Thread #608901. Printed from Allegro.cc