in reply to Joining a Thread in w While loop

Check out Thread::Cancel. Alternatively, use select to multiplex between checking for the $die flag and reading input. Thirdly, use Gtk's IO dispatcher instead of threads.

Pseudo-code for the select solution:

my $sel = new IO::Select; $sel->add($sock); while (1) { if ($sel->can_read(1)) { # 1 second timeout # do read, appending to buffer sysread($sock, $buf, 1024, length($buf)); # check for a complete line, process command, etc. } last if ($die); }

Update: Why can't you detach the thread? You're not returning any values from it. If you need to you can have it signal its completion by setting a shared variable.

Replies are listed 'Best First'.
Re^2: Joining a Thread in w While loop
by deadpickle (Pilgrim) on Mar 05, 2008 at 15:31 UTC
    The reason I didnt want to detach the thread is because this is for the IRC client that I had to recode using Parse::IRC. I cant print to the $sock in the main code line making the user unable to send a message to the server. If I could get the while loop to be non-blocking then I could send a signal that tells the thread to send a message to the server (which now that I think about seems primitive). I may try the  select option but I have 1 question: how do you check for a complete line?
      To check for a complete line just use a regex:
      if ($buf =~ s/(.*\n)//) { # line is in $1 my $line = $1; ...process $line here... }

      Note that the s operator conveniently removes the first complete line from $buf as well as returns whether there was a complete line found or not.

      btw, have you considered using the Net::IRC module? Have a look at this example: http://jk0.org/projects/perl-irc-bot/

        I have considered that but will it work with the GTK2 environment? most of these methods work but the problem is using them with GTK2 when you try to send something to the server. Parse::IRC was a real simple way to do this but I cant get it to work in GTK2 (since it seems that timeouts and IO's never work for me) so I'm going to give Net::IRC a run and see if that works better (though I'm not hopeful).