in reply to Re^4: Thread weirdness
in thread Thread weirdness

Caveat: Most of the contents of this post will only relate to you if you are using Win32. (Although the first bit about not being able to send on a socket from one thread whilst another thread is attempting to read from it may also be true on *nix for blocking sockets?)

The problem is that your are trying to both read and write to $Sock concurrently from two threads:

In your main thread:

while(1){ $Line = <$Sock>;

And in your Web thread, several instances of: &send_msg($Channel,"WEB FUNCTION THREAD STARTED"); which becomes:

print $Sock "PRIVMSG $_[0] :$_[1]\r\n";

As your threads are blocking, the writes SendMsg() is blocked until the read in the main thread completes. (Ie. some input arrives).

One solution is to set your socket non-blocking. (If your using Win32 Super search for '0x8004667e' for the collective wisdom on Win32 non-blocking sockets). However, this has a fairly profound knock-on for the architecture of your app.

A better solution is to only enter a read state when you know that there is something to be read. Unfortunately, the Win32 API that permits this is not directly available from Perl. Even if you access it directly (via: Win32::API), obtaining the appropriate Win32 system handle required to use it, , from the Perl level sockets, is entirely non-trivial.


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".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^6: Thread weirdness
by j0nny (Initiate) on Sep 26, 2008 at 14:59 UTC
    $Sock = new IO::Socket::INET(PeerAddr=>"$Server:$Port",Proto=>"tcp"); my $non = 1; ioctl($Sock,0x8004667e,\$non); $Sock->autoflush(); die("Couldn't connect: $!\n") unless $Sock;
    Did not do anything.
        Enable the socket to be written to while being read (Isn't that the problem?).