Re: Best Practice For Killing Thread?

[email protected] (Mike Pomraning)
Newsgroups perl.ithreads
Message-ID <[email protected]>
On Mon, 3 Nov 2003, Steve Schein wrote:

> What is the current best practice for killing a single thread with
> ithreads?

In this case, I think alteration of program logic is well-suited, esp. since
the thread's loop is already I/O driven.  This:

  while (defined $udp->read($buf, 8192)) {  # Assuming IO::Socket obj.
    ...
  }

becomes something like:

  pipe(R, W); # pipe to control processing thread

  # UDP processing thread
  async {

    close W;  # don't need it, must close it.

    while (1) {
      for my $fh (IO::Select->new($udp, \*R)->can_read) {
        if ($fh == \*R) {
          $udp->close;
          return;      # all done
        }
        handle_new_data($udp);
      }
    }
  };

  # back to main thread
  close(R);   # don't need it.


To get the processing thread to quit, close W.  can_read() will notice and
return the read-end of the pipe.

Alternatively, you could send yourself a special message over $udp (e.g.,
print {$udp} "QUIT!\n";), but that's not without security considerations, of
course.

Regards,
Mike
-- 
Michael J. Pomraning
[email protected]
http://pilcrow.madison.wi.us
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.