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

Alexandre Andrade alexandre.andrade at ecetera.com.au
Sun Oct 19 21:27:02 PDT 2014


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 <http://www.linkedin.com/company/ecetera>,
Twitter <http://www.twitter.com/#!/EceteraAU>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20141020/6609954f/attachment.html>


More information about the Melbourne-pm mailing list