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


in reply to Thread::Queue and objects

I am now trying to find another way to terminate nicely a thread.

Your code is kind of complicated for me to get my mind around( without work :-) ). All I can think of is to remind you, that to terminate a thread, the execution pointer in the thread must be sent to the end of it's code block. I resort to sending a shared variable to the thread, which tells it to "goto END", where the END: label is placed at the end of the thread code block.


I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^2: Thread::Queue and objects
by bronto (Priest) on Jan 14, 2005 at 15:11 UTC

    Ok, but... how would you make goto END a thread that is stuck in blocking call (a socket accept in this case)?

    Ciao!
    --bronto


    In theory, there is no difference between theory and practice. In practice, there is.
      Caveat: I'm not sure exactly what code you are referring to, and I havn't tested this. But I'm guessing you mean the following worker thread code block, which I would try to modify like the following. If you are blocking somewhere on a socket, maybe setup a timeout alarm on the connection, or use sysread or one of the other tricks to prevent blocking. All you really need to do, is somehow keep the thread looping, and periodically checking for $die.
      #in your main share $die; $die = 0; .... #set $die =1 when you want to exit or kill the thread ...... #worker code block sub httpd { LISTEN: { my $client = $httpd->accept ; goto END if $die; redo LISTEN unless defined $client ; my $request = $client->get_request ; unless ($request->method eq 'POST' and $request->url->path eq '/message') { $client->send_error(RC_FORBIDDEN) ; $client->close ; goto END if $die; redo LISTEN ; } my $q = CGI->new($request->content) ; my ($nick,$message) = map $q->param($_),qw(nick message) ; print STDERR "Resetting text box state\n" if DEBUG ; $tbox->configure(-state => 'normal') ; $tbox->insert('end',qq($nick says: $message\n)) ; print STDERR "Disabling text box\n" if DEBUG ; $tbox->configure(-state => 'disabled') ; $client->send_status_line ; $client->close ; goto END if $die; redo LISTEN ; } END: }

      I'm not really a human, but I play one on earth. flash japh