http://www.perlmonks.org?node_id=1122993


in reply to Re^5: ithreads memory leak
in thread ithreads memory leak

I've looked over both examples and while the second certainly looks easier, I agree that creating and destroying threads constantly is a waste compared to just starting two workers and sending them work.

I mostly get where you are going with this, but the one thing that I can't visualize is how are we going to disregard the open timer if the user closes the showcase? The work has already been passed to the thread. Also the walk timer needs to respect multiple key presses and update the expiry time, and cease termination and "restart" work.

Replies are listed 'Best First'.
Re^7: ithreads memory leak
by BrowserUk (Patriarch) on Apr 09, 2015 at 21:20 UTC
    I can't visualize is how are we going to disregard the open timer if the user closes the showcase? The work has already been passed to the thread. Also the walk timer needs to respect multiple key presses and update the expiry time, and cease termination and "restart" work.

    Then we need to be able to ask the thread to do two things:

    1. Add (or re-add) a showcase: $Qwalktimer->enqueue( "add:$showcasenum" );
    2. Remove an exist showcase: $Qwalktimer->enqueue( "del:$showcasenum" );

    And that means adding a test : if( exists $showcase{ $showcasenum }  ) { for an showcase number;

    And keeping a record of where the current timer for that showcase is: %showcase.

    Now, when 'add'ing a showcase, we remove any existing timer before adding a new one. And if the cmd eq 'del', then we just remove the existing timer.

    sub walktimer { ## The queue handle is passed in when the thread is started my $Q = shift; #Sleep for 180 seconds, upon completion execute SQL to change OK_O +PEN back to all zero for $showcasenum ## A hash to remember when things time out. my( %when, %showcase ); ## wake up once per second while( sleep 1 ) { ## while there are showcase numbers on the queue while( $Q->pending ) { ## grab them one at a time my( $cmd, $showcasenum ) = split ':', $Q->dequeue(); ## If a timer already exists for this showcase if( exists $showcase{ $showcasenum } ) { my $when = $showcase{ $showcasenum }; ## remove the timer @{ $when{ $when } } = grep{ $_ != $showcasenum } @{ $w +hen{ $when } }; ## and the pointer to it. delete $showcase{ $showcasenum }; ## And if this command is a delete request that's all next if $cmd eq 'del'; } ## Otherwise its a (re)add, so add the new timer ## Calculate when the timeout should occur, my $timeout = time() + $walktime; ## and add the showcase number to the hash to be dealt wit +h at that time push @{ $when{ $timeout } }, $showcasenumber; ## And remember its there $showcase{ $showcasenum } = $timeout; } ## find any times that have expired (earlier than now!) for my $time ( grep{ $_ < time() } keys %when ) { ## And for each showcase number scheduled at that time for my showcasenum ( @{ $when{ $time } } ) { ## Do your db stuff my $dbh = DBI->connect('dbi:mysql:alarm:databasehost', +'username','password') or die "Connection Error: $DBI::errstr\n"; my $sql = "update Showcases set OK_OPEN=0 WHERE SC_NUM + = ?"; my $sth = $dbh->prepare($sql) or die "Can't prepare $s +ql: $dbh->errstr\n"; $sth->execute($showcasenum) or die "SQL Error: $DBI::e +rrstr\n"; $dbh->disconnect; ## remove the pointer for this showcase delete $showcase{ showcasenum }; } ## and remove the expired time from the hash delete $when{ $time }; } } }

    Does that make sense? Does it cover all the bases?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      Crystal clear, I should have no issues implementing this in both threads now.

      Thanks so much for everything from start to finish, I'll consider this fixed but I'll know once I finish hammering this out tomorrow.