<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Yielding until an event occurs</title>
		<link>http://www.allegro.cc/forums/view/553251</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 21 Dec 2005 17:06:11 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>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 <i>rest( 0 );</i> is not what I am trying to achieve, but rather I am trying to achieve the same effect as calling the Windows API function <i>WaitMessage();</i>. If this is not possible with Allegro, then I must ask: why?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter .)</author>
		<pubDate>Fri, 16 Dec 2005 17:06:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why not:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/keypressed" target="_blank"><span class="a">keypressed</span></a> <span class="k2">(</span><span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k3">!</span><a href="http://www.allegro.cc/manual/mouse_b" target="_blank"><span class="a">mouse_b</span></a><span class="k2">)</span> <span class="k2">{</span>
  <a href="http://www.allegro.cc/manual/poll_keyboard" target="_blank"><span class="a">poll_keyboard</span></a> <span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/poll_mouse" target="_blank"><span class="a">poll_mouse</span></a> <span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a> <span class="k2">(</span><span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

About timer interrupt:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">volatile</span> <span class="k1">int</span> tick <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="k1">void</span> timer <span class="k2">(</span><span class="k1">void</span><span class="k2">)</span> <span class="k2">{</span> tick<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span> <span class="k2">}</span> <a href="http://www.allegro.cc/manual/END_OF_FUNCTION" target="_blank"><span class="a">END_OF_FUNCTION</span></a> <span class="k2">(</span>timer<span class="k2">)</span><span class="k2">;</span>

<span class="k1">void</span> fn <span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>
<span class="k2">{</span>
<span class="c">/* Whait for time interrupt. */</span>
  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a> <span class="k2">(</span>tick<span class="k2">)</span><span class="k2">;</span> <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a> <span class="k2">(</span>timer<span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/install_int_ex" target="_blank"><span class="a">install_int_ex</span></a> <span class="k2">(</span>timer, MSEC_TO_TIMER <span class="k2">(</span><span class="n">5000</span><span class="k2">)</span><span class="k2">;</span> <span class="c">/* Five seconds. */</span>
  <span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span>tick<span class="k2">)</span>
    <a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a> <span class="k2">(</span><span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="c">/* Uninstall time interrupt. */</span>
  <a href="http://www.allegro.cc/manual/remove_int" target="_blank"><span class="a">remove_int</span></a> <span class="k2">(</span>timer<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

About the &quot;why&quot; you ask: because Allegro is multi platform library and not all platforms manages events in the same way.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Niunio)</author>
		<pubDate>Fri, 16 Dec 2005 17:15:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>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&#39;t given execution time at all), until an event occurs? The problem with <i>rest( 0 );</i> is that the Allegro process is eventually given execution time again, even though no event has occured. Do you see what I&#39;m saying? I don&#39;t see how it would hinder portability, a function <i>wait_for_event</i> could exist, which would do the same thing as the Windows API&#39;s <i>WaitMessage();</i>, only if the platform of execution was incapable of doing it, it would simply do nothing (an empty function).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter .)</author>
		<pubDate>Fri, 16 Dec 2005 17:28:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Have you tried <span class="source-code"><a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a><span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></span>?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Fri, 16 Dec 2005 17:30:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think that actually this is impossible using only Allegro. Remember that Allegro is a <i>Game Library</i> and it isn&#39;t <i>multi threaded</i>. May be when Allegro 5.0 becomes released::)...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Niunio)</author>
		<pubDate>Fri, 16 Dec 2005 17:33:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I haven&#39;t tested this, but perhaps you could wait on a semaphore for signals that are sent on a mouse or keyboard event.</p><p>You can setup a callback functions that send a signal to the semaphore with <a href="http://www.allegro.cc/manual/api/mouse-routines/mouse_callback">mouse_callback</a> and <a href="http://www.allegro.cc/manual/api/keyboard-routines/keyboard_lowlevel_callback">keyboard_lowlevel_callback</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lennaert van der linden)</author>
		<pubDate>Fri, 16 Dec 2005 17:59:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Doesn&#39;t anyone ever RTFM <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>rest<br />Waits a specified number of milliseconds or yields CPU. <br />Description<br />void rest(unsigned int time); <br />This function waits for the specified number of milliseconds.</p><p>Passing 0 as parameter will not wait, but just yield.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Fri, 16 Dec 2005 18:56:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>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
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/keypressed" target="_blank"><span class="a">keypressed</span></a> <span class="k2">(</span><span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k3">!</span><a href="http://www.allegro.cc/manual/mouse_b" target="_blank"><span class="a">mouse_b</span></a><span class="k2">)</span>
  <a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a><span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
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).</p><p>In 4.3, however, there is going to be an event API that&#39;ll supposedly do exactly what you want. I&#39;ve never worked with it myself so I don&#39;t know its details, though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sat, 17 Dec 2005 04:59:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Also note that 4.3 has just begun mainstream development...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Sat, 17 Dec 2005 05:24:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oh, I didn&#39;t realise that <i>rest</i> can sleep the process, I thought it still ate CPU time while resting. Sure, it&#39;s not as efficient and &quot;clean&quot; (because during the resting period an event could occur and the task wouldn&#39;t handle it until the period is over) as an event-driven solution to this would be, but that&#39;s unimportant, this solution gets the job done fairly well.</p><p>Thanks everybody.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter .)</author>
		<pubDate>Sat, 17 Dec 2005 06:48:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>4.0&#39;s rest doesn&#39;t sleep BTW. You need yield_timeslice or something like that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (MiquelFire)</author>
		<pubDate>Sat, 17 Dec 2005 08:46:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>4.2&#39;s rest() however does. 4.0&#39;s yielding/resting behaviour is crooked anyway.<br />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&#39;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.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Sat, 17 Dec 2005 18:16:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In your main loop, use a rest(1) to reduce your CPU consumption to minimum, whilst still retaining responsiveness.</p><p>I highly disagree with using rest(50), it is far too high.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (A J)</author>
		<pubDate>Sat, 17 Dec 2005 19:30:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>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.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Sat, 17 Dec 2005 19:34:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>rest(50)  would give you considerable delay in response.</p><p>rest(1) is suffecient, it gives you both reduced cpu usage, and responsiveness,<br />(and on Windows, you&#39;ll get rest(10) anyway, due to the pathetically defaulted granuality)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (A J)</author>
		<pubDate>Sun, 18 Dec 2005 04:38:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>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.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jeff Bernard)</author>
		<pubDate>Tue, 20 Dec 2005 10:04:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>if your app is still at 77%, then you are obviously using 77% of the CPU for doing something.<br />if your using 77% of your CPU, its time to upgrade.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (A J)</author>
		<pubDate>Tue, 20 Dec 2005 20:34:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you&#39;re using 77% of cpu respurces, it&#39;s time to profile. If everything is optimized to the max and you&#39;re still using 77%, then it&#39;s time to upgrade (either OS or CPU or both).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Wed, 21 Dec 2005 17:06:11 +0000</pubDate>
	</item>
</rss>
