<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Multithreading and events</title>
		<link>http://www.allegro.cc/forums/view/610332</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 31 May 2012 16:37:34 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m really new to multithreading, I&#39;ve looked at the tutorial in the wiki. And I&#39;ve got a few questions.</p><p>1. a mutex is used so only one thread can read / write on whatever is between </p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_lock_mutex"><span class="a">al_lock_mutex</span></a><span class="k2">(</span>mutex<span class="k2">)</span>

stuff

<a href="http://www.allegro.cc/manual/al_unlock_mutex"><span class="a">al_unlock_mutex</span></a><span class="k2">(</span>mutex<span class="k2">)</span>
</pre></div></div><p>

And it&#39;s only used when reading / writing shared resources like the data class in the example, right?</p><p>2. What I want to do is have a thread which handles all input, but I can&#39;t get it to work. I tried adding an ALLEGRO_EVENT ev, to the data class. and copy the ALLEGRO_EVENT in the main loop to the ALLEGRO_EVENT in the data class. like this:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_wait_for_event"><span class="a">al_wait_for_event</span></a><span class="k2">(</span>event_queue, <span class="k3">&amp;</span>ev<span class="k2">)</span><span class="k2">;</span>

<a href="http://www.allegro.cc/manual/al_lock_mutex"><span class="a">al_lock_mutex</span></a><span class="k2">(</span>data.mutex<span class="k2">)</span><span class="k2">;</span>
data.ev <span class="k3">=</span> ev<span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_unlock_mutex"><span class="a">al_unlock_mutex</span></a><span class="k2">(</span>data.mutex<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Is there anything wrong with this? </p><p>3. In the input_thread function I&#39;ve got:
</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/al_get_thread_should_stop"><span class="a">al_get_thread_should_stop</span></a><span class="k2">(</span>thr<span class="k2">)</span><span class="k2">)</span><span class="k2">{</span>
  <a href="http://www.allegro.cc/manual/al_lock_mutex"><span class="a">al_lock_mutex</span></a><span class="k2">(</span>data-&gt;mutex<span class="k2">)</span><span class="k2">;</span>
        blah blah 
        <a href="http://www.allegro.cc/manual/al_unlock_mutex"><span class="a">al_unlock_mutex</span></a><span class="k2">(</span>data-&gt;mutex<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
When the mutex is locked in the main loop, and I&#39;m copying the ev to the data.ev, is the thread waiting for the mutex to be unlocked in the main before continuing with &quot;blah blah&quot;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnamra)</author>
		<pubDate>Thu, 31 May 2012 15:32:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>1. You can use mutexes to tell other threads to not touch the data protected by the mutex.</p><p>2. If you want to send events to another thread, the other thread should have an event queue listening to that kind of events.</p><p>3. If you want to lock a mutex that is already locked, the thread will wait until it&#39;s unlocked.</p><p>EDIT: here&#39;s a good read on multithreading: <a href="http://msdn.microsoft.com/en-us/magazine/cc163744.aspx">http://msdn.microsoft.com/en-us/magazine/cc163744.aspx</a> (I know it uses .NET, but the same principles apply in every multithreaded environment)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Thu, 31 May 2012 15:45:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Which tutorial are you referring to?</p><p>In general, something like your code under 3. makes no sense. Using a mutex means you force the code inside to be single threaded again. So if you lock your whole loop body - why are you using an extra thread in the first place? Threads are for when you want to run things in parallel.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Thu, 31 May 2012 15:58:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oh, thanks for the help! I&#39;ll try adding an event_queue to the data class.</p><p>Also, this is the tutorial I was referring to: <a href="http://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Threads">http://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Threads</a></p><p>in there he locked the entire loop as well:</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/al_get_thread_should_stop"><span class="a">al_get_thread_should_stop</span></a><span class="k2">(</span>thr<span class="k2">)</span><span class="k2">)</span><span class="k2">{</span>
 
      <a href="http://www.allegro.cc/manual/al_lock_mutex"><span class="a">al_lock_mutex</span></a><span class="k2">(</span>data-&gt;mutex<span class="k2">)</span><span class="k2">;</span>
      <span class="k1">if</span><span class="k2">(</span>modi_X<span class="k2">)</span>
         data-&gt;posiX <span class="k3">+</span><span class="k3">=</span> num<span class="k2">;</span>
      <span class="k1">else</span>
         data-&gt;posiY <span class="k3">+</span><span class="k3">=</span> num<span class="k2">;</span>
      <a href="http://www.allegro.cc/manual/al_unlock_mutex"><span class="a">al_unlock_mutex</span></a><span class="k2">(</span>data-&gt;mutex<span class="k2">)</span><span class="k2">;</span>
 
      <a href="http://www.allegro.cc/manual/al_rest"><span class="a">al_rest</span></a><span class="k2">(</span><span class="n">0</span>.<span class="n">01</span><span class="k2">)</span><span class="k2">;</span>
 
   <span class="k2">}</span>
</pre></div></div><p>

and it makes sense to me, because if you don&#39;t lock it while messing with the data-&gt;posiX, then any other thread could mess with it while the first thread is? And since I&#39;m using events, I don&#39;t want the event to change in the middle of checking what event type it is. Perhaps I&#39;m just really misunderstanding the whole thing.</p><p>Edit: Oh, I&#39;m not sending them. I&#39;m just copying the info in one over to the other in the data class.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnamra)</author>
		<pubDate>Thu, 31 May 2012 16:14:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If a thread wants to listen to events, it has to have its own event queue, on which the events you want it to listen to are registered.</p><p>The example locks the mutex on the whole thread because the only thing that thread does is change things from the shared data. In general, you only want mutexes around the parts where you access the shared data. If that&#39;s the only thing you do, you should probably don&#39;t do it in a separate thread. Where threads come in most handy is with AI for example. It makes a bunch of calculations and then puts it results in the shared data for the main game to pick up. It will only have mutexes around the parts where it reads/writes data to the shared memory, not where it does its AI logic.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Thu, 31 May 2012 16:23:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I see, and again thanks for the help <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /> I got it working, and a much better understanding of how it all works. I&#39;m going to do some more experimentation. Also that article was very useful. </p><p>Edit: I&#39;ve read through about half of the article and experimented a bit, and I&#39;m wondering what would happen if:</p><p>i is a global variable.</p><p>thread 1:<br />i = 25;<br />al_lock_mutex(m)<br />i = 50;<br />al_unlock_mutex(m);</p><p>thread 2:<br />std::cout &lt;&lt; i &lt;&lt; std::endl;</p><p>now assuming thread 2 is trying access the memory location of i while mutex has locked that memory location. I&#39;m guessing it either gives an error or exception.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnamra)</author>
		<pubDate>Thu, 31 May 2012 16:37:34 +0000</pubDate>
	</item>
</rss>
