<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Need Good tutorials on Allegro threads.</title>
		<link>http://www.allegro.cc/forums/view/617700</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 19 Jan 2019 22:49:23 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi guys.</p><p>I was trying to use ALLEGRO_THREAD in my C program but I just couldn&#39;t figure it out that how it actually works.</p><p>Edit:<br />Is there a way to pass more than one structure in thread function?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Doctor Cop)</author>
		<pubDate>Thu, 17 Jan 2019 21:21:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What are you planning to use threads for? Or are you just trying to learn?</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> Composite <span class="k2">{</span>
   MyStuff<span class="k3">*</span> a<span class="k2">;</span>
   OtherStuff<span class="k3">*</span> b<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 17 Jan 2019 22:42:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have used pthreads before but I&#39;m having difficulty in following examples at GitHub repository.</p><p>A comparison of both will be good.<br />I was thinking of making a small pong game as my krampus hack disaster compensation.<br />I am making a basic pong game for now but I will improve it with time and add elements to it gradually.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Doctor Cop)</author>
		<pubDate>Thu, 17 Jan 2019 23:22:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Multi threaded pong? What on earth for?</p><p>Threads are for parallel processing. There&#39;s nothing in pong that would benefit from using a thread.</p><p>See ex_threads.c :</p><p><a href="https://github.com/liballeg/allegro5/blob/master/examples/ex_threads.c">https://github.com/liballeg/allegro5/blob/master/examples/ex_threads.c</a><br /><a href="https://github.com/liballeg/allegro5/blob/master/examples/ex_threads2.c">https://github.com/liballeg/allegro5/blob/master/examples/ex_threads2.c</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 17 Jan 2019 23:30:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Then how will I catch input and still move ball at the same time?</p><p>al_wait_for_event() and al_rest() will cause problem.<br />When I&#39;m waiting for event I can&#39;t update my game loop.<br />What should I do then?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Doctor Cop)</author>
		<pubDate>Fri, 18 Jan 2019 03:07:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Use a timer to wake up the queue. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>See the example of a generic game loop on the wiki :</p><p><a href="https://github.com/liballeg/allegro_wiki/wiki/Allegro-Vivace%3A-Basic-game-structure">https://github.com/liballeg/allegro_wiki/wiki/Allegro-Vivace%3A-Basic-game-structure</a></p><p>Click on View Source near the top to see an example.</p><p>Basic event loop is like this :</p><p>Psuedocode :
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span>quit<span class="k2">)</span> <span class="k2">{</span>
   <span class="k1">if</span> <span class="k2">(</span>redraw<span class="k2">)</span> <span class="k2">{</span>Redraw<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>redraw <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span><span class="k2">}</span>
   <span class="k1">do</span> <span class="k2">{</span>
      WaitForEvent<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
      <span class="k1">if</span> <span class="k2">(</span>TimerEvent<span class="k2">)</span> <span class="k2">{</span>Logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>redraw <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span><span class="k2">}</span>
   <span class="k2">}</span> <span class="k1">while</span> <span class="k2">(</span>HaveEvent<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>

<span class="k1">void</span> Logic<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
   ball.x <span class="k3">+</span><span class="k3">=</span> vx<span class="k2">;</span>
   ball.y <span class="k3">+</span><span class="k3">=</span> vy<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 18 Jan 2019 05:13:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Just for the sake of completeness:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span>
<span class="number">  2</span><span class="k1">struct</span> FOO <span class="k2">{</span>
<span class="number">  3</span>
<span class="number">  4</span>    <span class="k1">struct</span> A a<span class="k2">;</span>
<span class="number">  5</span>    <span class="k1">struct</span> B b<span class="k2">;</span>
<span class="number">  6</span>
<span class="number">  7</span><span class="k2">}</span><span class="k2">;</span>
<span class="number">  8</span>
<span class="number">  9</span>
<span class="number"> 10</span><span class="k1">void</span> process_struct_foo<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a> <span class="k3">*</span>t, <span class="k1">void</span> <span class="k3">*</span>arg<span class="k2">)</span>
<span class="number"> 11</span><span class="k2">{</span>
<span class="number"> 12</span>
<span class="number"> 13</span>    <span class="k1">struct</span> FOO <span class="k3">*</span>foo  <span class="k3">=</span> <span class="k2">(</span><span class="k1">struct</span> FOO <span class="k3">*</span><span class="k2">)</span> arg<span class="k2">;</span>
<span class="number"> 14</span>    do_stuff_with_a<span class="k2">(</span><span class="k3">&amp;</span>foo-&gt;a<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span>    do_stuff_with_b<span class="k2">(</span><span class="k3">&amp;</span>foo-&gt;b<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span>
<span class="number"> 17</span><span class="k2">}</span>
<span class="number"> 18</span>
<span class="number"> 19</span>
<span class="number"> 20</span><span class="k1">void</span> do_threading<span class="k2">(</span><span class="k1">struct</span> FOO <span class="k3">*</span>foo<span class="k2">)</span>
<span class="number"> 21</span><span class="k2">{</span>
<span class="number"> 22</span>
<span class="number"> 23</span>    <a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a> <span class="k3">*</span>t <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_thread"><span class="a">al_create_thread</span></a><span class="k2">(</span>process_struct_foo, foo<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 24</span>    <a href="http://www.allegro.cc/manual/al_start_thread"><span class="a">al_start_thread</span></a><span class="k2">(</span>t<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 25</span>    <a href="http://www.allegro.cc/manual/al_join_thread"><span class="a">al_join_thread</span></a><span class="k2">(</span>t, NULL<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 26</span>
<span class="number"> 27</span><span class="k2">}</span>
</div></div><p>

Void casting is usually something we try to avoid but in this instance, we sort of have to. C puts the developer in the driver&#39;s seat, so you&#39;d double-down better know what you&#39;re doing. Also, in real code, you would manually destroy the structs so as not to be leaking memory.</p><p>One caveat: BE CAREFUL with multithreading <i>pointers.</i> If you have a pointer inside a struct, which in turn points to some data you&#39;re messing with, and two (or more) threads manipulate that data, you create a </p><h3>race condition.</h3><p>

[Ominous music plays in background]</p><p>Race conditions are a quick road to undefined behavior, where lie dragons and madness. You literally have NO IDEA what the hardware will do!!</p><p>You certainly <i>can</i> multithread pointers, and lots of good code does this, but be warned: only one piece of code manipulating on one chunk of data at a time.</p><p>For the love of all that you hold dear, <b>avoid the dread race condition!</b>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (princeofspace)</author>
		<pubDate>Fri, 18 Jan 2019 06:23:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Don&#39;t join the thread until you&#39;re done.</p><p>Also, shared data requires the use of synchronization or Mew Texas. (mutexes).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 18 Jan 2019 06:36:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617700/1040876#target">Edgar Reynaldo</a> said:</div><div class="quote"><p>
Don&#39;t join the thread until you&#39;re done.</p><p>Also, shared data requires the use of synchronization or Mew Texas. (mutexes).
</p></div></div><p>

Good catch, I forgot to write that stuff in the quick example code. In most cases, Allegro already does all the practical threading for you. If you&#39;re using the event queue system or music streams, Allegro is threading away in the background.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (princeofspace)</author>
		<pubDate>Fri, 18 Jan 2019 08:33:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks.</p><p>It worked.<br />Now I&#39;m going to add controls and later will try to implement multiplayer system.</p><p>Edit:<br />The multiplayer system was easily implemented but how to implement com part.<br />How the game AI should work in pong?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Doctor Cop)</author>
		<pubDate>Sat, 19 Jan 2019 18:58:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Think. What does a player do? That&#39;s at least one option for the AI to choose.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 19 Jan 2019 22:49:23 +0000</pubDate>
	</item>
</rss>
