[Melbourne-pm] Use of each() on hash after insertion without resetting hash interator results in undefined behavior

Dean Hamstead dean at fragfest.com.au
Sun Oct 19 21:46:36 PDT 2014


 

Hash keys are in "random" order, so when a new one is inserted that
order changes. 

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. 

Adding threads makes this more strange, as you absolutely have a race
condition without locking. 

You are probably better off pushing on to a stack (array) and popping
each one off for processing. 

my @stack= ({ options => 'foo' }, {}, {}, {}); 

while (1) { 

 my $foo = pop @stack or last; 

 workon($foo) 

} 

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 

Dean 

On 2014-10-20 15:27, Alexandre Andrade wrote: 

> Hi Folks, 
> I'm using Perl v5.20.0 for sun4-solaris-thread-multi-64 on Solaris 11. 
> 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. 
> Something like this: 
> 
> # This Thread will run in paralel and check the Memory Hash for completed transactions and do some tricks 
> my $thr = threads->new( &check_time ); 
> while (defined(my $line=$file->read)) {
> 
> my @transaction_details : shared; 
> . 
> . 
> . 
> $transactions{ $transaction->{"RequestId"} } = @transaction_details;
> 
> }
> 
> sub check_time() { 
> 
> while (1) { 
> { 
> foreach my $key ( keys(%transactions) ) { 
> 
> doSomeMagic( $transactions{$key} ); 
> delete($transactions{$key}); 
> # Abandoning the Foreach Loop as I've changed the %transactions memory map 
> last; 
> } 
> sleep(1); 
> }
> 
> }
> 
> -- 
> The code works but the warning: 
> 
> Use of each() on hash after insertion without resetting hash interator results in undefined behavior 
> 
> 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. 
> 
> Any ideas? 
> 
> Regards, 
> 
> Alexandre Andrade | APM Consultant | Ecetera alexandre.andrade at ecetera.com.au | M: +61 41307 1370 
> 
> Helping rid the World of badly behaving Apps by investigating my Customers problems and finding solutions to make things work faster and better. 
> Connect to Ecetera on LinkedIn [2], Twitter [3]
> 
> _______________________________________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm [1]
 

Links:
------
[1] http://mail.pm.org/mailman/listinfo/melbourne-pm
[2] http://www.linkedin.com/company/ecetera
[3] http://www.twitter.com/#!/EceteraAU
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20141020/138a3a1b/attachment-0001.html>


More information about the Melbourne-pm mailing list