<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Allegro 5 threads</title>
		<link>http://www.allegro.cc/forums/view/608306</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 18 Sep 2011 12:47:16 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey,</p><p>I&#39;m pretty new to using threads in allegro 5.0, and now i wanted to use them in my little 2d Pokemon game. I&#39;ve looked for some toturials and the only usefull toturial i was able to find was: <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>Now i had a few question about this:</p><p>1) How to properly use them? I mean is it possible to draw Bitmaps inside the Thread? I tried to do this but i failed. I was able to create a new working thread, and tried to use it for loading the graphics but it was terrible slow.</p><p>2) Are there more usefull toturials i could look at for multithreading in allegro 5?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (julian smit)</author>
		<pubDate>Sun, 11 Sep 2011 20:23:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>1) only the thread that makes the display can load/draw video bitmaps. All other threads use software bitmaps, and those are slooowwwww...</p><p>2) You could search the forum for multithreading here on the forum. I have learned a lot from those threads. Otherwise, just try and understand the example programs.(play with their code a bit and you might get to understand threads better)<br />You might also want to look at some online tutorials about multithreading to get to know what threads are generally used for, what the popular design patterns are,...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Sun, 11 Sep 2011 20:29:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ah okay thanks for the help, i also thought it had something todo with the display, and i guess i could look at the way other languanes use the threads.<br />thanks!.</p><p>There&#39;s only 1 question though: i&#39;m trying to load all the data in 1 thread (graphics and fonts etc.. (map files later)) anyway i&#39;m able the fonts but when i try to load the graphics they program doesn&#39;t crash but i can see the text but the graphics are all black and the program is lagging like hell any ideas why?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (julian smit)</author>
		<pubDate>Sun, 11 Sep 2011 22:25:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Do you mean that you are loading them in a seperate thread? If so, bitmaps won&#39;t load with hardware acceleration, thus making the program terribly slow. I have no idea why they would get blacked out though... Did you properly initialize allegro?(al_init_image_addon() etc.)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Sun, 11 Sep 2011 23:25:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You need to do all your resource loading in the main thread or you&#39;ll get memory bitmaps and may have some strange problems. Display status / progress reports / whatever in the secondary threads.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Mon, 12 Sep 2011 06:01:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608306/930659#target">julian smit</a> said:</div><div class="quote"><p>
How to properly use them?
</p></div></div><p>

</p><ul><li><p>separate the simulation logic from the presentation logic.
</p></li><li><p>update the simulation in a worker thread.
</p></li><li><p>when the worker thread is ready, send the simulation state to the presentation thread, i.e. the main thread.
</p></li><li><p>the main thread then draws the current simulation state.
</p></li><li><p>UI commands from the main thread should be sent to the simulation thread as messages.</p></li></ul><p>Generally, think about the problem has having two programs cooperating: one program does the work, the other displays the work. They communicate by messages.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Mon, 12 Sep 2011 16:09:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay thanks for the help so far, now i&#39;m trying to make a loop in the main thread but when i try this the other thread isn&#39;t functionating anymore any ideas/help on how i can fix this?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (julian smit)</author>
		<pubDate>Mon, 12 Sep 2011 20:20:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It should work... could you give us some code?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Mon, 12 Sep 2011 20:29:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>EDIT: question in new post.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (julian smit)</author>
		<pubDate>Mon, 12 Sep 2011 20:36:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Do you see that in thread 2, you got a nested loop? you&#39;ve got one in Thread_2 AND one in NewMain. Thread 1 isn&#39;t working because thread 2 never releases the mutex on data(until you exit the NewMain loop)</p><p>APPEND: I don&#39;t see why your code compiles... main() doesn&#39;t return.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Mon, 12 Sep 2011 21:29:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>that&#39;s true i was thinking that was the problem but when i didn&#39;t use the loop the program shut down. also i&#39;ve started from the example in the allegro wiki again. my only question now is: </p><p>I want to use the main thread for drawing everything, and use the (child) thread for loading everything files,bitmaps etc. now the only problem is that it isn&#39;t possible to load bitmaps in a thread except the thread where the display is created?</p><p>EDIT: nvm i was able to load them in the other thread thanks anyway!.</p><p>EDIT2: When i try to load a map file (without multithreading) the program freezes for a few seconds, now i try to load the file in the second thread now the second thread freezes but the main thread also freezes. any ideas why? although when i use the al_rest(); function in the thread it doesn&#39;t slow down the main thread.</p><p>EDIT3: I was able to fix it by not using the al_lock_mutex (and unlock) in the main thread ( i didn&#39;t need it anyway for updating the values)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (julian smit)</author>
		<pubDate>Mon, 12 Sep 2011 21:37:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><s>You can draw to a display from a thread but that thread has to <i>own</i> the display. Allegro lets you change ownership of a display using the `al_set_target_backbuffer` function. If you created the Display in one thread and want to use it in another, you first have to `al_set_target_backbuffer(0);` in the creating thread, then do `al_set_target_backbuffer(display);` in the thread that wants to use that display. This means only one thread can draw to a display at any one time.</s></p><p>If you want to load bitmaps in a separate thread, make them memory bitmaps, then make the main thread <span class="source-code"><a href="http://www.allegro.cc/manual/al_clone_bitmap"><span class="a">al_clone_bitmap</span></a></span> them all as display bitmaps. Loading bitmaps in a secondary thread will cause those bitmaps to not be connected to that display, and make them very slow to draw.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 12 Sep 2011 22:46:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You should only use mutexes if you are trying to access data that&#39;s shared with other threads. Don&#39;t unnecessarily lock a mutex.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Mon, 12 Sep 2011 23:58:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ah, that clears alot up, although i got one last question your saying that i only should lock the mutex when it&#39;s sharing data, what kind of data do you mean and why should i use it than anyway?.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (julian smit)</author>
		<pubDate>Tue, 13 Sep 2011 00:43:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That&#39;s what that DATA class is for. You put all variables that the threads share in there, and you put lock/unlock_mutex calls at places where you want to access those variables.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Tue, 13 Sep 2011 14:31:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ah okay thanks i got everything working now although i got one kind of a stupid question, because when i want to load files i normally just would call a void in the main thread for example, although that&#39;s now not possible so when i want to load a file for the main thread but don&#39;t want to make it freeze i do something like. if(data.load_map == true) map_load(data.map_id); in the (child) thread and in the main thread put the data.load_map = true; are there easier ways to do this? because this seems like a pretty bad solution..
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (julian smit)</author>
		<pubDate>Tue, 13 Sep 2011 21:28:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/608306/930919#target">julian smit</a> said:</div><div class="quote"><p> Ah okay thanks i got everything working now although i got one kind of a stupid question, because when i want to load files i normally just would call a void in the main thread for example, although that&#39;s now not possible so when i want to load a file for the main thread but don&#39;t want to make it freeze i do something like. if(data.load_map == true) map_load(data.map_id); in the (child) thread and in the main thread put the data.load_map = true; are there easier ways to do this? because this seems like a pretty bad solution..</p></div></div><p>Is it so difficult to write English properly? to use punctuation properly? I had to read your text 3 times in order to understand what you ask.</p><p>If you want help, then you should ask for it in a proper way.</p><p>Anyway, back to your problem: if you want to program any time-consuming job without having to freeze the main thread, you have to do it in a worker thread. When the worker thread finishes, then it notifies the main thread by an event.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Wed, 14 Sep 2011 16:07:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Sorry for that, <br />Anyway i&#39;m pretty happy with what i&#39;ve done so far, i&#39;m able to load almost everything in the worker thread, and actually everything is running fine except when the first thread is using the display, and i want to load the graphics in the the second thread the screen doesn&#39;t show up normally but goes like <br />black-normal-black-normal for a few mseconds. any ideas how to fix this? <br />Sorry for my bad english anyway.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (julian smit)</author>
		<pubDate>Sat, 17 Sep 2011 13:16:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Don&#39;t load graphics in a thread that didn&#39;t create the display. It makes it go to memory bitmaps, instead of hardware-accelerated video bitmaps.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Sat, 17 Sep 2011 13:20:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>so that means you always have to load the graphics in the presentation thread?<br />because than the presentation thread still freezes for a second when i load a map that needs new graphics.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (julian smit)</author>
		<pubDate>Sat, 17 Sep 2011 13:32:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The thread that renders the screen should also load graphics... I don&#39;t know a workaround for that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (J-Gamer)</author>
		<pubDate>Sat, 17 Sep 2011 13:41:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can use al_clone_bitmap/al_convert_bitmap from the main thread (like ex_loading_thread in SVN does): <a href="https://github.com/elias-pschernig/allegro5/blob/master/examples/ex_loading_thread.c">https://github.com/elias-pschernig/allegro5/blob/master/examples/ex_loading_thread.c</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Sat, 17 Sep 2011 20:55:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why <span class="source-code">al_convert_bitmap</span> function name is so generic? I haven&#39;t read the specifications of this function but automatically means that convert from memory to video bitmap? or it&#39;s like a switch? if is a MB convert it to VB? and vice versa?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (AMCerasoli)</author>
		<pubDate>Sun, 18 Sep 2011 05:29:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It converts the bitmap to the current settings, much like:
</p><div class="source-code snippet"><div class="inner"><pre>bmp <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_clone_bitmap"><span class="a">al_clone_bitmap</span></a><span class="k2">(</span>bmp<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
The main difference is that the original pointer is still valid.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Sun, 18 Sep 2011 05:42:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>thanks, that helped alot.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (julian smit)</author>
		<pubDate>Sun, 18 Sep 2011 12:47:16 +0000</pubDate>
	</item>
</rss>
