in reply to Re: [threads] Sharing object through threads
in thread [threads] Sharing object through threads

Cool, that seams to work but now another problem appears. i thought i could resolve the problem by sharing an object but it seams i cannot. because in threaded server application it looks like the object shared is type of a globe ($client == IO::Socket::INET=GLOB(0x189a908)) which is unsharable for now.
#!/usr/bin/perl -w use strict; use IO::Socket; use threads; use threads::shared; my $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $ARGV[0], Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; share(my @jobs); while (my $client = $server->accept()) { $client->autoflush(1); print "[Connect from ". $client->peerhost . ":" . $client->peerpo +rt . "]\n" ; # clients address my $thr = threads->new(sub { push(@jobs,share($client)); ### NOTE THIS IS UNS +HARABLE ### while (<$client>) { if (/test/i){ print $client "looks good, @jobs\n"; } elsif (/quit/i){ print $client "bye\n"; last; } } # finishe while } )->detach; close $client; }
since you had the similar problem couple a years ago i was wondering if you found the solution or.... (sharing globs)

thank you

so the idea is when one client talks server listens and shares its responses with all connected clients

Replies are listed 'Best First'.
Re^3: [threads] Sharing object through threads
by BrowserUk (Patriarch) on Nov 17, 2009 at 19:27 UTC

    The situation is that I never succeeded in working out how to make threads::shared share globs.

    I got quite a long way towards that goal, but always found something that prevented my attempts from being completely successful, and did not know enough about the guts of Perl magic to be able to debug it when it went wrong. I remain convinced that if someone with the requisite skills could be recruited to spend a litte time trying, it could be achieved. But those with the requisite skills do not consider threads a high priority it seems. Which is a shame, because IMO, this is the single most limiting factor on the usefulness of threads in perl.

    I do not have a clean alternative for you.


    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.
Re^3: [threads] Sharing object through threads
by ikegami (Patriarch) on Nov 17, 2009 at 19:58 UTC

    Sockets are nothing but file handles. Share the file handle number (fileno($client)) and recreate the object on the other side.

    Catch: You can't close the socket in the parent.

      Recreating the socket is trivial. Recreating all the stuff that the various IO:* modules attach to the glob is far less simple.


      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.
        True, but who cares. He has a freshly accepted socket, so all he needs is
        sub new_inet_from_fd { my ($fd, %arg) = @_; my $timeout = delete $arg{Timeout}; my $blocking = exists $arg{Blocking} ? delete $arg{Blocking} : 1; my $sock = IO::Socket::INET->IO::Handle::new_from_fd($fd, '+<'); ${*$sock}{'io_socket_timeout'} = $timeout; ${*$sock}{io_sock_nonblocking} = !$blocking; $sock->blocking($blocking); $sock->autoflush(1); return $sock; }

        It produces a socket whose operations don't timeout (unless one is specified), whose operations do block (unless otherwise specified), and that does autoflush. Everything in IO::Handle other than autoflush is at its default.