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

package Foo; use threads; use threads::shared; sub new { my $class = shift; my $self : shared; $self = &share({}); $self->{socket} = &share(new IO::Socket::INET ( PeerAddr => $self->{ipAddress}, PeerPort => $self->{port}, Proto => 'tcp' )); bless $self, $class; return $self
this code gives me the error - Argument to share needs to be passed as ref. Am I doing something stupid or is it not possible to share a socket between threads. Thanks.

Replies are listed 'Best First'.
Re: Sharing sockets between threads
by zentara (Cardinal) on Mar 19, 2009 at 11:49 UTC
      Cool! How do I get the socket object from its fileno?
        get the socket object from its fileno

        I'm sure there is a backtracking trail you could go thru, via some socket utility, like socklist, but I think your best bet is to just store the info in a hash, like

        %client; $client{$id}{$sock} = $socket; $client{$id}{'fileno'} = fileno( $socket );
        then all you need to do is track id values.

        I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness