<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Random number generator + multithreading problem</title>
		<link>http://www.allegro.cc/forums/view/618379</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 05 Mar 2021 04:46:23 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello.<br />I moved my AI evaluation function to run on a separated thread and now all random number generator calls returns the same number.<br />On the main thread everything is fine.</p><p>My RNG function:
</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">int</span> iRand<span class="k2">(</span><span class="k1">int</span> min, <span class="k1">int</span> max<span class="k2">)</span>
<span class="number"> 2</span><span class="k2">{</span>
<span class="number"> 3</span>  <span class="k1">if</span> <span class="k2">(</span>min <span class="k3">&gt;</span><span class="k3">=</span> max<span class="k2">)</span> <span class="k1">return</span> min<span class="k2">;</span>
<span class="number"> 4</span>  <span class="k1">int</span> i <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_637.html" target="_blank">rand</a><span class="k2">(</span><span class="k2">)</span>%<span class="k2">(</span>max <span class="k3">-</span> min <span class="k3">+</span> <span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 5</span>  <span class="k1">return</span> min <span class="k3">+</span> i<span class="k2">;</span>  
<span class="number"> 6</span><span class="k2">}</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Space cpp)</author>
		<pubDate>Thu, 04 Mar 2021 06:00:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Are you initializing the random numbers with <a href="http://www.cplusplus.com/reference/cstdlib/srand/">srand</a>?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Thu, 04 Mar 2021 06:04:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Perhaps you need to reseed it on each thread?</p><p><span class="source-code"><span class="k1">void</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_739.html" target="_blank">srand</a><span class="k2">(</span><span class="k1">unsigned</span><span class="k2">)</span><span class="k2">;</span></span></p><p>Or seed it at all. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Most people pass time as the seed value so it changes each time you run the program.</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.delorie.com/djgpp/doc/libc/libc_739.html" target="_blank">srand</a><span class="k2">(</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a><span class="k2">(</span>NULL<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Thu, 04 Mar 2021 06:51:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I was calling srand(time(NULL)) at the beggining of the program. Calling it on the thread function solved the problem.</p><p>But... the program crashed on exit. <br />Do I have to use a mutex to ensure the safety of the srand/rand calls? I&#39;m still kinda new to multithreaded programming.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Space cpp)</author>
		<pubDate>Thu, 04 Mar 2021 07:58:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hard to know what caused the crash without seeing the code. Can you share some?</p><p>Multithreaded programming is complex, and the question you should always ask is, do I really need it? You might be able to do without, and it will simplify things a lot.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (amarillion)</author>
		<pubDate>Thu, 04 Mar 2021 18:27:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Even if it&#39;s not strictly necessary in this case, it can still be a good occasion to learn/experiment.</p><p>In this case, I have a hard time imagining why having called <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_637.html" target="_blank">rand</a><span class="k2">(</span><span class="k2">)</span> <span class="k3">/</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_739.html" target="_blank">srand</a><span class="k2">(</span><span class="k2">)</span></span> would lead to a crash later.<br />I would rather advise you double-check every freeing of resource that your child thread might still be using.</p><p>For example, if your AI thread uses any allegro function / data, the thread should already be stopped before allegro is shut down.<br />Note that even if you don&#39;t call <span class="source-code"><a href="http://www.allegro.cc/manual/al_uninstall_system"><span class="a">al_uninstall_system</span></a><span class="k2">(</span><span class="k1">void</span><span class="k2">)</span></span> yourself, it will be called after you reach the end of <span class="source-code">main<span class="k2">(</span><span class="k2">)</span></span> (AFAIK it is using the <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_52.html" target="_blank">atexit</a><span class="k2">(</span><span class="k2">)</span></span> system to do so)</p><p>In general, killing the thread should be one of the first things to do when you stop the program.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Thu, 04 Mar 2021 19:13:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://">amarillion</a> said:</div><div class="quote"><p>Hard to know what caused the crash without seeing the code. Can you share some?</p></div></div><p>
Sorry, the code is way too huge to post.</p><div class="quote_container"><div class="title"><a href="http://">amarillion</a> said:</div><div class="quote"><p>the question you should always ask is, do I really need it? You might be able to do without, and it will simplify things a lot. </p></div></div><p>
My game runs fine without it, but on really large maps the A.I. tends to take almost 1 second to calculate every move, making the whole game appears frozen. That&#39;s why I think using a separated thread for it became necessary.</p><div class="quote_container"><div class="title"><a href="http://">Audric</a> said:</div><div class="quote"><p>In general, killing the thread should be one of the first things to do when you stop the program. </p></div></div><p>
I call al_destroy_thread on my leave game routine, so it doesn&#39;t seem to be the case.</p><p>I&#39;m testing the game again and not getting any chashes, it must have been a coincidence that something went wrong right after including the new srand call.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Space cpp)</author>
		<pubDate>Fri, 05 Mar 2021 04:46:23 +0000</pubDate>
	</item>
</rss>
