<div dir="ltr">Dean/Toby,<div>Thank you a lot for you replies !</div><div><br></div><div>Dean, very clear explanation! But In this case I need to check if the values of the Hash item is complete (the main thread writes several times to each item before I can use it) so if I pop I have to push it back in case it is incomplete - And thanks for sharing Gavin's material - a most read !</div><div><br></div><div>Toby,</div><div>Very well observed but I already have to lock command around the Hash interactions  - I've cleaned them up to keep the code clear, that was a poor choice. </div><div>I have a test case where I generate sequences, consume then using this scrip and compare the output with another processes and even after 20 hours of execution I got not fails - the only issue was that warning popping for most of the time - I was using a older version of Perl and I don't remember that message showing up. </div><div><br></div><div>As I couldn't get rid of that and I am compiling Perl from source, I've decide to just comment that line from the "hv.c" and I will let my test code run for a few days to guarantee I'm not hiding a bug.</div><div><br></div><div>Cheers,</div><div>Alex</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 20, 2014 at 3:52 PM, Toby Corkindale <span dir="ltr"><<a href="mailto:toby.corkindale@strategicdata.com.au" target="_blank">toby.corkindale@strategicdata.com.au</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Andre,<br>
In my experience, Perl threads don't work properly. Not just my opinion either --the official stance is that their use is "strongly discouraged".<br>
<br>
To avoid the error you're seeing, I think you'd need a mutex lock around the hash access, ie. so that the foreach loop can't run at the same time as the hash insertion -- except that if you're going to do that, then your program may as well be single threaded.<br>
Consider rewriting your code to use forking with a queue if you're after performance and reliability.<br>
If you do stick with threads, then you also need to take more care around race conditions - for instance, consider what happens if a requestid is added to the hash for a second time, while the first time is being processed in doSomeMagic().<br>
<br>
Cheers,<br>
Toby<br>
<span class=""><br>
----- Original Message -----<br>
> From: "Alexandre Andrade" <<a href="mailto:alexandre.andrade@ecetera.com.au">alexandre.andrade@ecetera.com.au</a>><br>
> To: <a href="mailto:melbourne-pm@pm.org">melbourne-pm@pm.org</a><br>
> Sent: Monday, 20 October, 2014 3:27:02 PM<br>
> Subject: [Melbourne-pm] Use of each() on hash after insertion without resetting hash interator results in undefined<br>
> behavior<br>
><br>
</span><span class="">> Hi Folks,<br>
> I'm using Perl v5.20.0 for sun4-solaris-thread-multi-64 on Solaris 11.<br>
> I have a main thread that reads from disk and adds some values to a Shared<br>
> Hash and a secondary thread that goes through that Hash, uses some values<br>
> and delete the used values from the hash.<br>
> Something like this:<br>
><br>
> # This Thread will run in paralel and check the Memory Hash for completed<br>
> transactions and do some tricks<br>
> my $ thr = threads -> new ( \ &check_time );<br>
> while ( defined ( my $ line = $ file -> read )) {<br>
> my @ transaction_details : shared ;<br>
> .<br>
> .<br>
> .<br>
> $ transactions { $ transaction ->{ "RequestId" } } = \ @ transaction_details<br>
> ;<br>
> }<br>
> sub check_time () {<br>
> while ( 1 ) {<br>
> {<br>
> foreach my $ key ( keys ( % transactions ) ) {<br>
> doSomeMagic( $ transactions { $ key } );<br>
</span>> delete ( $ transactions { $ key });<br>
<span class="">> # Abandoning the Foreach Loop as I've changed the %transactions memory map<br>
> last ;<br>
> }<br>
> sleep(1);<br>
> }<br>
> }<br>
><br>
> --<br>
> The code works but the warning:<br>
><br>
> Use of each() on hash after insertion without resetting hash interator<br>
> results in undefined behavior<br>
><br>
> Is flooding the logs - I've tried to set "no warnings" or create a copy of<br>
> %transactions and interact with it instead of %transaction itself but no<br>
> luck so far.<br>
><br>
> Any ideas?<br>
><br>
> Regards,<br>
><br>
> Alexandre Andrade | APM Consultant | Ecetera<br>
> <a href="mailto:alexandre.andrade@ecetera.com.au">alexandre.andrade@ecetera.com.au</a> | M: +61 41307 1370<br>
><br>
> Helping rid the World of badly behaving Apps by investigating my Customers<br>
> problems and finding solutions to make things work faster and better.<br>
</span>> Connect to Ecetera on LinkedIn , Twitter<br>
<div class="HOEnZb"><div class="h5">><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
> _______________________________________________<br>
> Melbourne-pm mailing list<br>
> <a href="mailto:Melbourne-pm@pm.org">Melbourne-pm@pm.org</a><br>
> <a href="http://mail.pm.org/mailman/listinfo/melbourne-pm" target="_blank">http://mail.pm.org/mailman/listinfo/melbourne-pm</a><br>
_______________________________________________<br>
Melbourne-pm mailing list<br>
<a href="mailto:Melbourne-pm@pm.org">Melbourne-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/melbourne-pm" target="_blank">http://mail.pm.org/mailman/listinfo/melbourne-pm</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr"><br><div>Regards,</div><div><br></div><div><div style="color:rgb(136,136,136)"><span style="color:rgb(102,102,102)">Alexandre Andrade | APM Consultant | Ecetera</span><br></div><font size="1" color="#666666"><a href="mailto:alexandre.andrade@ecetera.com.au" style="color:rgb(17,85,204)" target="_blank">alexandre.andrade@ecetera.com.au</a> | M: +61 41307 1370<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 href="http://www.linkedin.com/company/ecetera" style="color:rgb(17,85,204)" target="_blank">LinkedIn</a>, <a href="http://www.twitter.com/#!/EceteraAU" style="color:rgb(17,85,204)" target="_blank">Twitter</a><br><br><br></div></font></div><div><p style="margin:0px"></p><p style="margin:0px"></p></div></div>
</div>