开发者

Thread joining problems

am using Perl on a linux box and my memory usage is going up and up, I believe because of previously run threads that have not been killed/joined. I think I need to somehow signal the thread(s) that have done/run to terminate, and then detach it/them so that it/they will get cleaned up automatically giving me back memory...

I have tried return(); with $thr_List->join(); & $thr_List->detach(); but my gui doesn't show for ages with join, and the mem problem seems to be still there with the detach... Any help...

$mw->repeat(100, sub { # shared var handler/pivoting-point !?

while (defined(my $command = $q->dequeue_nb())) { #???
# to update a statusbar's text
        $text->delete('0.0', "end");
        $text->insert('0.0', $开发者_StackOverflow中文版command);
  $indicatorbar->value($val); # to update a ProgressBar's value to $val
  $mw->update();

    for ( @threadids ) { # @threadids is a shared array containing thread ids
    # that is to say I have got the info I wanted from a thread and pushed its id into the above @threadids array. 
    print "I want to now kill or join the thread with id:  $_\n";
        #$thrWithId->detach();
        #$thrWithId->join();
    # then delete that id from the array 
    # delete $threadids[elWithThatIdInIt];


    # as this seems to be in a repeat(100, sub... too, there are problems??!
    # locks maybe?!? 
    # for ( @threadids ) if its not empty?!?
        }
   } # end of while
}); # end of sub

# Some worker... that works with the above handler/piviot me thinks#???
async {
   for (;;) {
 sleep(0.1);
 $q->enqueue($StatusLabel);
   }
 }->detach();

I have uploaded my full code here (http://cid-99cdb89630050fff.office.live.com/browse.aspx/.Public) if needed, its in the Boxy.zip...


First sorry for replying here but I've lost my cookie that allows editing etc...

Thanks very much gangabass, that looks like great info, I will have to spend some time on it though, but at least it looks like others are asking the same questions... I was worried I was making a total mess of things.

Thanks guys...


So it sounds like you got the join working but it was very slow?

Threading in perl is not lightweight. Creating and joining threads takes significant memory and time. If your task allows, it is much better to keep threads running and give them additional work rather than ending them and starting new threads later. Thread::Queue can help with this. That said, unless you are on Windows, there is not a lot of point to doing that instead of forking and using Parallel::ForkManager.


You need to use only one Worker thread and update GUI from it. Other threads just process data from the queue and there is no need to terminate them.

See this example for more info

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜