Sako <riderjunk@NOSPAMdls.net> wrote:[color=blue]
> I am trying to teach myself perl by writing a program I've been meaning
> to implement, so I am pretty green in perl. I'm having problems sharing
> filehandles opened by a thread - been RTFM for a few days, but am having
> no luck.[/color]
I think Perl is a great language, but not for threading. You should
probably either choose a different task to be your introduction to Perl;
or, if threading is central to what you do, then choose to learn a
different language.
[color=blue]
> I am attempting to write a threaded server program that listens on a
> socket for requests, then passes the socket's filehandle to an event
> processor routine, while the listener thread keeps on listening.
> However, I cannot seem to be able to successfully pass the filehandle
> from the listener thread to the event handler.[/color]
If you are going to start a new thread for each session (which I wouldn't
recommend, but then again I don't recommend this whole situation), then I
wouldn't pass the handle at all. Simply stash the handle in a variable
whose scopes spans both the listener and the event handler. A child thread
glombs onto whatever handle was stored in a variable at the time it was
created. This requires the listener to be the "master" thread.
use strict;
use warnings;
use threads;
use threads::shared;
$|=1;
my $conn;
foreach (1..10) {
undef $conn;
open $conn, ">/tmp/foo_$_" or die $!;
threads->create("do_it", $_);
};
$_->join foreach threads->list();
sub do_it {
my $request=shift;
foreach (1..100)
{
print $conn "$request $_\n";
select undef,undef,undef, 0.1;
};
};
Xho
--
--------------------
http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB