zentara has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to write to all threads on a multi-threaded INET server. I basically have it going, but I've run into a situation where I'm trying to extract the $client name from a reference, and can't seem to find the way. Now I'm wondering if it's even possible. The code is long, and rather than waste disk space, I will just show the basic code and the Dumper outputs.

I'm creating 3 threads each with a socket connection. When I save the $clients in @clients, I can loop thru the @clients array and print to each $client. But when I try to save $thr to @thrs, I cannot get the $client back out to print to it. I can get the thread's tid, but that's it.

So my question is this:

Can I get the thread's $client name from the information provided in the Dump of @thrs? For instance, I can loop through @clients and print to each thread. But how do I convert the @thrs to usable $client names in order to print to it?

for(1..3){ $client = $listener->accept; my $thr = threads->create(\&start_thread, $client, ++$client_ +num); push(@clients,$client); push(@thrs,$thr); print 'Dump of @clients-> ',Dumper([@clients]),"\n"; print 'Dump of @thrs-> ',Dumper([@thrs]),"\n"; } ############################################## Dump of @clients-> $VAR1 = [ bless( \*Symbol::GEN1, 'IO::Socket::INET' ), bless( \*Symbol::GEN2, 'IO::Socket::INET' ), bless( \*Symbol::GEN3, 'IO::Socket::INET' ) ]; Dump of @thrs-> $VAR1 = [ bless( do{\(my $o = '137520752')}, 'threads' ), bless( do{\(my $o = '138536664')}, 'threads' ), bless( do{\(my $o = '138556504')}, 'threads' ) ];

Replies are listed 'Best First'.
Re: dereferencing IO::Socket::INET from a thread
by bschmer (Friar) on Nov 21, 2003 at 16:20 UTC
    Couldn't you just save the clients as a hash keyed by $thr instead of as an array? I would think that this would take care of your problem by tracking the information in the main thread as opposed to getting it back from each of the individual threads.
      Yes, I've done it that way, with a %thr hash, and it works like you suggest. But it only works from the main program. What I'm eventually aiming at is to print to the other threads from a thread. When I try to use threads::shared on @clients or %thrs, and try to access them from each thread, each thread dosn't see a thread created after it was created. But each thread will return the same list if I call threads->list. But here's the gotcha, the list returned in each thread from threads->list, is of the form in @thrs , which presents the same dereferencing problem, which I'm asking. I hope I made sense.