<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Question regarding multithreading</title>
		<link>http://www.allegro.cc/forums/view/610871</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 22 Aug 2012 11:15:50 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have never worked with multi-threading before (started learning about it a few days ago). it looked interesting and like a powerful tool, so i started messing around with it. Making a few test programs here and there. </p><p>In my latest test I have hit a roadblock that I can&#39;t seem to get myself around. </p><p>I have located the problem, but i am unaware as to why it is giving me a run-time-error. here is the code for the thread that is running and causing the error (note: this function works if not using it in a thread): </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="k1">void</span> <span class="k3">*</span>Load<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a> <span class="k3">*</span>thr,  <span class="k1">void</span> <span class="k3">*</span>arg<span class="k2">)</span>
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>  std::cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Load thread: Started."</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> std::endl<span class="k2">;</span>
<span class="number">  4</span>
<span class="number">  5</span>  ThreadInfo <span class="k3">*</span>Info <span class="k3">=</span> <span class="k2">(</span>ThreadInfo<span class="k3">*</span><span class="k2">)</span> arg<span class="k2">;</span>
<span class="number">  6</span> 
<span class="number">  7</span>  <a href="http://www.allegro.cc/manual/al_get_bitmap_format"><span class="a">al_get_bitmap_format</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_backbuffer"><span class="a">al_get_backbuffer</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_current_display"><span class="a">al_get_current_display</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span>  <span class="c">//LoadMap();</span>
<span class="number">  9</span>
<span class="number"> 10</span>  <a href="http://www.allegro.cc/manual/al_lock_mutex"><span class="a">al_lock_mutex</span></a><span class="k2">(</span>Info-&gt;mutex<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>  Info-&gt;State-&gt;CurState <span class="k3">=</span> Playing<span class="k2">;</span>
<span class="number"> 12</span>  <a href="http://www.allegro.cc/manual/al_unlock_mutex"><span class="a">al_unlock_mutex</span></a><span class="k2">(</span>Info-&gt;mutex<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span>
<span class="number"> 14</span>  std::cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Load thread: Ended."</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> std::endl<span class="k2">;</span>
<span class="number"> 15</span>
<span class="number"> 16</span>  <span class="k1">return</span> NULL<span class="k2">;</span>
<span class="number"> 17</span><span class="k2">}</span>
</div></div><p> </p><p>The line that it gives the error on is:</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_get_bitmap_format"><span class="a">al_get_bitmap_format</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_backbuffer"><span class="a">al_get_backbuffer</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_current_display"><span class="a">al_get_current_display</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

I&#39;m guessing it is because the call to al_get_bitmap(...) is not thread safe. But I&#39;m still new to this multi-threading stuff. So if someone could confirm or deny, that be great. And any ideas on how to make this work using threads would also be appreciated.  </p><p>Thanks in advanced,<br />-Ryan
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ryancoboy)</author>
		<pubDate>Wed, 22 Aug 2012 08:16:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The first thing you should do is check the return value of each function before passing it to the next.
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a> <span class="k3">*</span> display<span class="k2">;</span>
<a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span> bitmap<span class="k2">;</span>
<span class="k1">int</span> format<span class="k2">;</span>

display <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_current_display"><span class="a">al_get_current_display</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">if</span><span class="k2">(</span>display<span class="k2">)</span>
<span class="k2">{</span>
    bitmap <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_backbuffer"><span class="a">al_get_backbuffer</span></a><span class="k2">(</span>display<span class="k2">)</span><span class="k2">;</span>
    <span class="k1">if</span><span class="k2">(</span>bitmap<span class="k2">)</span>
    <span class="k2">{</span>
        format <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_bitmap_format"><span class="a">al_get_bitmap_format</span></a><span class="k2">(</span>bitmap<span class="k2">)</span><span class="k2">;</span>
    <span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>

Most likely, one of these functions is returning <span class="source-code">NULL</span> and causing a crash when that value is passed to a function expecting a valid pointer.</p><p>Also, in your code you aren&#39;t storing the format anywhere. What exactly are you trying to do there?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Todd Cope)</author>
		<pubDate>Wed, 22 Aug 2012 09:11:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Todd Cope: Thanks for the tips I&#39;ll try that here in a couple moments and post back here (dinner time <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />). As you can see, I have a function commented out &quot;LoadMap()&quot; and deep in that function is the al_get_bitmap_format(...) code. i just moved it up front so that i wouldn&#39;t have to post a billion function calls, etc. Once a solution is found to prevent the error I will attempt to fix it where it is actually intended to be used.    </p><p>EDIT: I just tested it real quick. And &quot;format&quot; ended up being equal to 23. So it looks to be running fine there as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ryancoboy)</author>
		<pubDate>Wed, 22 Aug 2012 09:37:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The manual states that each thread has it&#39;s own &quot;current display&quot;, and new threads have a default NULL current display, so you&#39;re passing NULL into those functions and that&#39;s probably why it&#39;s crashing.</p><p>If you had done what Todd said, you would have figured that out.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 22 Aug 2012 10:06:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Trent Gamblin: your right. I must of missed that part in the manual or forgot about it. Thanks for clearing that up for me. And i was in a rush for my dinner so i didn&#39;t test the return value of my display in my &quot;Load&quot; thread. I just did it in my main. I was rushing and didn&#39;t read the post completely. Sorry, for that. </p><p>That pretty much solves my problem. </p><p>Thanks guys.<br />-Ryan
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ryancoboy)</author>
		<pubDate>Wed, 22 Aug 2012 11:15:50 +0000</pubDate>
	</item>
</rss>
