<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html><body style='font-family: Verdana,Geneva,sans-serif'>
<p>Hash keys are in "random" order, so when a new one is inserted that order changes.</p>
<p>When iterating through a hash (via each() etc), perl marks its spot when it moves from key to key. But when a new key is added, the spot that it marked may not have the same keys before and after it as prior to the addition.</p>
<p>Adding threads makes this more strange, as you absolutely have a race condition without locking.</p>
<p>You are probably better off pushing on to a stack (array) and popping each one off for processing.</p>
<p>my @stack= ({ options => 'foo' }, {}, {}, {});</p>
<p>while (1) {</p>
<p>  my $foo = pop @stack or last;</p>
<p>  workon($foo)</p>
<p>}</p>
<p>Gavin Carr recently gave a talk in parallelising in perl at Sydney PM, see http://www.openfusion.net/talks/pwp/ The MCE module may be helpful</p>
<p> </p>
<p>Dean</p>
<p> </p>
<p>On 2014-10-20 15:27, Alexandre Andrade wrote:</p>
<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px"><!-- html ignored --><!-- head ignored --><!-- meta ignored -->
<div dir="ltr">Hi Folks,
<div>I'm using Perl v5.20.0 for sun4-solaris-thread-multi-64 on Solaris 11.</div>
<div>I have a main thread that reads from disk and adds some values to a Shared Hash and a secondary thread that goes through that Hash, uses some values and delete the used values from the hash.</div>
<div>Something like this:</div>
<div> </div>
<div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="color: #b20022; font-family: Courier New;"><span style="font-size: 10pt;"># This Thread will run in paralel and check the Memory Hash for completed transactions and do some tricks</span></span></div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="color: #a020f0; font-family: Courier New;"><span style="font-size: 10pt;">my <span style="color: black;">$</span><span style="color: #a000f0;">thr</span><span style="color: black;"> </span><span style="color: #b22200;">= </span><span style="color: black;">threads</span><span style="color: #b22200;">-></span>new<span style="color: #b22200;">( \</span><span style="color: black;">&check_time </span><span style="color: #b22200;">);</span></span></span></div>
</div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="color: #a020f0; font-size: 13.63636302948px;">while </span><span style="font-size: 13.6364px; color: #b22200;">(</span><span style="font-size: 13.6364px; color: #a000f0;">defined</span><span style="font-size: 13.6364px; color: #b22200;">(</span><span style="color: #a020f0; font-size: 13.63636302948px;">my </span><span style="font-size: 13.6364px; color: black;">$</span><span style="font-size: 13.6364px; color: #a000f0;">line</span><span style="font-size: 13.6364px; color: #b22200;">=</span><span style="font-size: 13.6364px; color: black;">$</span><span style="font-size: 13.6364px; color: #a000f0;">file</span><span style="font-size: 13.6364px; color: #b22200;">-></span><span style="font-size: 13.6364px; color: #a000f0;">read</span><span style="font-size: 13.6364px; color: #b22200;">)) {</span><span style="color: #a020f0; font-family: Courier New;"><span style="font-size: 10pt;"><span style="color: #b22200;"><br /></span></span></span></div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="font-size: 13.6364px; color: #b22200;">    </span><span style="font-size: 13.6364px; color: #a020f0;">my </span><span style="font-size: 13.63636302948px;">@</span><span style="font-size: 13.6364px; color: #a000f0;">transaction_details</span><span style="font-size: 13.63636302948px;"> </span><span style="color: #b22200; font-size: 13.63636302948px;">: </span><span style="font-size: 13.63636302948px;">shared</span><span style="color: #b22200; font-size: 13.63636302948px;">;</span></div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="color: #b22200; font-size: 13.63636302948px;">.</span></div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="color: #b22200; font-size: 13.63636302948px;">.</span></div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="color: #b22200; font-size: 13.63636302948px;">.</span></div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="font-size: 13.6364px; color: #a020f0;">         </span><span style="font-size: 13.63636302948px;">$</span><span style="font-size: 13.6364px; color: #a000f0;">transactions</span><span style="color: #b22200; font-size: 13.63636302948px;">{ </span><span style="font-size: 13.63636302948px;">$</span><span style="font-size: 13.6364px; color: #a000f0;">transaction</span><span style="color: #b22200; font-size: 13.63636302948px;">->{</span><span style="font-size: 13.6364px; color: blue;">"RequestId"</span><span style="color: #b22200; font-size: 13.63636302948px;">} } = \</span><span style="font-size: 13.63636302948px;">@</span><span style="font-size: 13.6364px; color: #a000f0;">transaction_details</span><span style="color: #b22200; font-size: 13.63636302948px;">;</span><span style="color: #b22200; font-size: 13.63636302948px;"><br /></span></div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="color: #b22200; font-size: 13.63636302948px;">}</span><span style="color: #b22200; font-size: 13.63636302948px;"><br /></span></div>
<div><span style="font-size: 13.6364px; color: #a020f0;">sub </span><span style="color: #000000; font-size: 13.63636302948px;">check_time</span><span style="font-size: 13.6364px; color: #b22200;">() {</span></div>
<div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="color: #b22200; font-family: Courier New;"><span style="font-size: 10pt;"><span style="color: #a020f0;">        while </span>(<span style="color: #a02000;">1</span>) {</span></span></div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="color: #b22200; font-family: Courier New;"><span style="font-size: 10pt;">        {</span></span></div>
<div style="color: #000000; font-family: Verdana; font-size: 16.3636360168457px;"><span style="color: #b22200; font-family: Courier New;"><span style="font-size: 10pt;">             <span style="color: #a020f0;">foreach my </span><span style="color: black;">$</span><span style="color: #a000f0;">key</span><span style="color: black;"> </span>( <span style="color: #a000f0;">keys</span>(<span style="color: black;">%</span><span style="color: #a000f0;">transactions</span>) ) {</span></span></div>
</div>
<div style="font-family: Verdana; font-size: 16.3636360168457px;">
<div style="font-size: 16.3636360168457px;"><span style="font-family: Courier New;"><span style="font-size: 10pt;"><span style="color: #000000;">                  doSomeMagic( </span><span style="color: black;">$</span><span style="color: #a000f0;">transactions</span><span style="color: #000000;">{</span><span style="color: black;">$</span><span style="color: #a000f0;">key</span><span style="color: #000000;">} );</span></span></span></div>
<div style="color: #000000; font-size: 16.3636360168457px;"><span style="font-family: Courier New;"><span style="font-size: 10pt;">                  <span style="color: #a000f0;">delete</span><span style="color: #b22200;">(</span>$<span style="color: #a000f0;">transactions</span><span style="color: #b22200;">{</span>$<span style="color: #a000f0;">key</span><span style="color: #b22200;">});</span></span></span></div>
<div style="color: #000000; font-size: 16.3636360168457px;"><span style="color: #b22200; font-family: Courier New;"><span style="font-size: 10pt;">                  </span></span><span style="color: #b20022; font-size: 10pt;"># Abandoning the Foreach Loop as I've changed the %transactions memory map</span></div>
<div style="color: #000000; font-size: 16.3636360168457px;"><span style="font-family: Courier New;"><span style="font-size: 10pt;">                  <span style="color: #a020f0;">last</span><span style="color: #b22200;">;</span></span></span></div>
<div style="color: #000000; font-size: 16.3636360168457px;"><span style="color: #b22200; font-family: Courier New;"><span style="font-size: 10pt;">              }</span></span></div>
<div style="color: #000000; font-size: 16.3636360168457px;"><span style="color: #b22200; font-family: Courier New;"><span style="font-size: 10pt;">              sleep(1);</span></span></div>
<div style="color: #000000; font-size: 16.3636360168457px;"><span style="color: #b22200; font-size: 13.63636302948px;">      }</span><span style="color: #b22200; font-family: Courier New;"><span style="font-size: 10pt;"><br /></span></span></div>
<div style="color: #000000; font-size: 16.3636360168457px;"><span style="color: #b22200; font-size: 13.63636302948px;">}</span><span style="color: #b22200; font-size: 13.63636302948px;"><br /></span></div>
</div>
<div>
<div> </div>
-- </div>
<div>The code works but the warning:</div>
<div> </div>
<div>Use of each() on hash after insertion without resetting hash interator results in undefined behavior</div>
<div> </div>
<div>Is flooding the logs - I've tried to set "no warnings"  or create a copy of %transactions and interact with it instead of %transaction itself but no luck so far. </div>
<div> </div>
<div>Any ideas?</div>
<div>
<div dir="ltr"><br />
<div>Regards,</div>
<div> </div>
<div>
<div style="color: #888888;"><span style="color: #666666;">Alexandre Andrade | APM Consultant | Ecetera</span></div>
<span style="color: #666666; font-size: xx-small;"><span style="color: #666666; font-size: xx-small;"><a style="color: #1155cc;" href="mailto:alexandre.andrade@ecetera.com.au">alexandre.andrade@ecetera.com.au</a> | M: +61 41307 1370</span></span>
<div><br />Helping rid the World  of badly behaving Apps by investigating my Customers problems and finding solutions to make things work faster and better. <br />Connect to Ecetera on <a style="color: #1155cc;" href="http://www.linkedin.com/company/ecetera">LinkedIn</a>, <a style="color: #1155cc;" href="http://www.twitter.com/#!/EceteraAU">Twitter</a><br /><br /><br /></div>
</div>
<div> </div>
</div>
</div>
</div>
<br />
<pre>_______________________________________________
Melbourne-pm mailing list
<a href="mailto:Melbourne-pm@pm.org">Melbourne-pm@pm.org</a>
<a href="http://mail.pm.org/mailman/listinfo/melbourne-pm">http://mail.pm.org/mailman/listinfo/melbourne-pm</a>
</pre>
</blockquote>
</body></html>