|
In a message dated 3/23/2006 2:29:26 P.M. Eastern Standard Time,
[EMAIL PROTECTED] writes:
> Hello,
> > I have a problem using a threaded TCP-server. Clients connect to > send a short message. This message is then processed. This processing > can take some time (seconds). Writing such a server for Linux is easy, > but Windows is different. I tried several examples and they all failed. > > This one keeps working, behaves the same on Linux (Perl 5.8.4) and > Windows (ActivePerl 5.8.8 816), but generates an error message with each > connect: > thread failed to start: Undefined subroutine &main::1 called at [reference > to $thr = threads->new(&process_request($client));] > > Does anyone know what is wrong? I don't see how a thread ends. > (Threading is not my thing, until this week I always forked.) > > Thanks, > momo > > --- > #!/usr/bin/perl > use strict; > use threads; > use IO::Socket; > use IO::Select; > > my $DONE = 0; > my $path = './'; > my ($thr); > > $SIG{INT} = $SIG{TERM} = sub { $DONE++ }; > > my $port = 1080; > my $socket = IO::Socket::INET->new( LocalPort => $port, > Type => SOCK_STREAM, > Listen => SOMAXCONN, > ReuseAddr => 1 > ) or die "Can't create listen socket: $!"; > my $IN = IO::Select->new($socket); > > while (!$DONE) { > next unless $IN->can_read; > next unless my $client = $socket->accept; > $thr = threads->new(&process_request($client)); hi momo --
just a guess, but it looks to me like the above call to new()
passes the return
value of a nested call to &process_request($client) and not
the name of
a function and a list of function parameters that new() expects
(see below).
in other words, new() wants a symbolic reference to a function
and not the
result of the invocation of a function.
since &process_request($client) returns the result of the
close() call
that is the last thing in &process_request($client) and
since close()
returns true (1) if successful, i think this is where the ``1'' is coming
from that is
being taken as the name of a function that cannot be found. so,
maybe you want
something like:
$thr = threads->new("process_request",
$client);
> $thr->detach;
> } > > ### process the request > sub process_request { > my $client = $_[0]; > local %ENV = (); > my $line = <$client>; > # > #do my thing > # > close $client; > } see ``perldoc threads'':
SYNOPSIS
use threads; sub start_thread
{
print "Thread started\n"; } my $thread =
threads->create("start_thread","argument");
...
DESCRIPTION
...
$thread = threads->create(function,
LIST)
This will create a new thread with the entry point function and give it LIST as parameters. It will return the corresponding threads object. The new() method is an alias for create(). hth -- bill walters
|
_______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
