Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Yielding until an event occurs

This thread is locked; no one can reply to it. rss feed Print
Yielding until an event occurs
Peter .
Member #6,687
December 2005

I was wondering if it is possible to yield a process using Allegro, until an event occurs (i.e. the press of a key, a timer interrupt, etc.). I have looked through the manual, and have not found anything like this. Calling rest( 0 ); is not what I am trying to achieve, but rather I am trying to achieve the same effect as calling the Windows API function WaitMessage();. If this is not possible with Allegro, then I must ask: why?

Niunio
Member #1,975
March 2002
avatar

Why not:

while (!keypressed () && !mouse_b) {
  poll_keyboard ();
  poll_mouse ();
  rest (0);
}

About timer interrupt:

volatile int tick = 0;
void timer (void) { tick++; } END_OF_FUNCTION (timer);

void fn (void)
{
/* Whait for time interrupt. */
  LOCK_VARIABLE (tick); LOCK_FUNCTION (timer);
  install_int_ex (timer, MSEC_TO_TIMER (5000); /* Five seconds. */
  while (!tick)
    rest (0);
/* Uninstall time interrupt. */
  remove_int (timer);
}

About the "why" you ask: because Allegro is multi platform library and not all platforms manages events in the same way.

-----------------
Current projects: Allegro.pas | MinGRo

Peter .
Member #6,687
December 2005

The problem there is that it will still eat CPU time, checking Task Manager under Windows shows the Allegro process consuming something like 98% of the CPU time, although all my process keeps doing is going through a loop checking if escape is pressed. I want to know how could I indefinitely put the process to sleep (so that it isn't given execution time at all), until an event occurs? The problem with rest( 0 ); is that the Allegro process is eventually given execution time again, even though no event has occured. Do you see what I'm saying? I don't see how it would hinder portability, a function wait_for_event could exist, which would do the same thing as the Windows API's WaitMessage();, only if the platform of execution was incapable of doing it, it would simply do nothing (an empty function).

miran
Member #2,407
June 2002

Have you tried rest(1);?

--
sig used to be here

Niunio
Member #1,975
March 2002
avatar

I think that actually this is impossible using only Allegro. Remember that Allegro is a Game Library and it isn't multi threaded. May be when Allegro 5.0 becomes released::)...

-----------------
Current projects: Allegro.pas | MinGRo

lennaert van der linden
Member #6,053
July 2005
avatar

I haven't tested this, but perhaps you could wait on a semaphore for signals that are sent on a mouse or keyboard event.

You can setup a callback functions that send a signal to the semaphore with mouse_callback and keyboard_lowlevel_callback

Neil Walker
Member #210
April 2000
avatar

Doesn't anyone ever RTFM ;)

rest
Waits a specified number of milliseconds or yields CPU.
Description
void rest(unsigned int time);
This function waits for the specified number of milliseconds.

Passing 0 as parameter will not wait, but just yield.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Kitty Cat
Member #2,815
October 2002
avatar

In 4.2 or earlier, there is no way to truely wait until an event occurs.. all you can do is make a check+rest combo. The loop

while(!keypressed () && !mouse_b)
  rest(1);

will wait (and give up most of the CPU) until a key or mouse button is pressed. For just keypresses, you can use readkey(), which will wait and give up the CPU until a key is pressed (by implementing its own loop).

In 4.3, however, there is going to be an event API that'll supposedly do exactly what you want. I've never worked with it myself so I don't know its details, though.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

CGamesPlay
Member #2,559
July 2002
avatar

Also note that 4.3 has just begun mainstream development...

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Peter .
Member #6,687
December 2005

Oh, I didn't realise that rest can sleep the process, I thought it still ate CPU time while resting. Sure, it's not as efficient and "clean" (because during the resting period an event could occur and the task wouldn't handle it until the period is over) as an event-driven solution to this would be, but that's unimportant, this solution gets the job done fairly well.

Thanks everybody.

MiquelFire
Member #3,110
January 2003
avatar

4.0's rest doesn't sleep BTW. You need yield_timeslice or something like that.

---
Febreze (and other air fresheners actually) is just below perfumes/colognes, and that's just below dead skunks in terms of smells that offend my nose.
MiquelFire.red | +Me
Windows 8 is a toned, stylish, polished professional athlete. But it’s wearing clown makeup, and that creates a serious image problem. ~PCWorld Article

Tobias Dammers
Member #2,604
August 2002
avatar

4.2's rest() however does. 4.0's yielding/resting behaviour is crooked anyway.
You may also want to use a coarser granularity if low resource consumption is more important to you than responsiveness. Just use something like rest(50) in you polling loop; this will only check the input 20 times per second. As long as you program is the foreground app, this is probably not acceptable, but as lomng as you're in pause mode or something like that and waiting for input is your only task, then a 50msec delay between hitting a key and your program responding is not likely to be noticed anyway.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

A J
Member #3,025
December 2002
avatar

In your main loop, use a rest(1) to reduce your CPU consumption to minimum, whilst still retaining responsiveness.

I highly disagree with using rest(50), it is far too high.

___________________________
The more you talk, the more AJ is right. - ML

Tobias Dammers
Member #2,604
August 2002
avatar

Read my post again; rest(50) should be fine if all you need to do is wait in the background until a key is pressed. In all other cases, rest(50) is absolutely insane of course.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

A J
Member #3,025
December 2002
avatar

rest(50) would give you considerable delay in response.

rest(1) is suffecient, it gives you both reduced cpu usage, and responsiveness,
(and on Windows, you'll get rest(10) anyway, due to the pathetically defaulted granuality)

___________________________
The more you talk, the more AJ is right. - ML

Jeff Bernard
Member #6,698
December 2005
avatar

it may just be my program, but i dont see much of a difference when implementing readkey() or rest(1). the peak is the same for using both of those and not using any of them, at 98% cpu usage. but the lowest point is achieved when neither is used, which got down to 72%. readkey() and rest(1) were both around 77% for their lowest.

--
I thought I was wrong once, but I was mistaken.

A J
Member #3,025
December 2002
avatar

if your app is still at 77%, then you are obviously using 77% of the CPU for doing something.
if your using 77% of your CPU, its time to upgrade.

___________________________
The more you talk, the more AJ is right. - ML

Tobias Dammers
Member #2,604
August 2002
avatar

If you're using 77% of cpu respurces, it's time to profile. If everything is optimized to the max and you're still using 77%, then it's time to upgrade (either OS or CPU or both).

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: