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

hello, here is some code i put together from some stuff i have been reading, and from some responses i got in another post. I just wanted to know how i could share a variable among threads, I thought that if i shared the $client variable, it would print the message on each of the clients, who are connected to the server. Can anybody explain how to do this to me? here is the code:
#!/usr/bin/perl use strict; use threads; use threads::shared; use IO::Socket::INET; $| ++; my $listener = IO::Socket::INET->new ( LocalPort => 1337, Listen => 5, Reuse => 1 ) || die "Cannot create socket\n"; warn "server up and ready for connections...... \n"; my $client : shared; my $client_num = 0; my @grab; while (1) { our $client = $listener->accept; threads->create(\&start_thread, $client, ++ $client_num); } sub start_thread { my ($client, $client_num) = @_; print "thread created for client $client_num\n"; push @grab, $client; print @grab; print $client "Welcome to the Test server!\n"; &begin; } sub begin { while(our $line = <$client>) { print $client $line; print $line; } }

Replies are listed 'Best First'.
Re: sharing variables with threads
by pg (Canon) on Mar 13, 2003 at 03:12 UTC
    You cannot share socket among threads, not becaue it is a socket. The real problem is that it is an object.

    In Perl 5.8.0, sharing object among threads is absolutely unsafe, as bless is not thread-safe / not implemented for threading (in 5.8.0). This is stated in document for threads::shared.

    Socket can be used on thread, that's not a problem, but can not be shared among threads.

    This is something they are working on, so your concept would work with future versions of Perl with no problem.

    Update

    jasonk, I have read your reply:
    1. What I stated in this post is conceptually correct. Let me repeat it: as for Perl 5.8.0, object can not be shared among threads. In future versions, after bless is fixed for multi-threading, socket could be shared among threads, with no difference from sharing other object.
    2. Don't jump to the conclusion that Anonymous Monk's concept in his mind is wrong, just because his code has some bugs.
    3. What you said about his code is not quite right, and I replied you with a detailed analysis.

      The concept would not work in future versions, as the basic concept is that he has a single filehandle ($client) that represents more than one socket, if sharing of filehandles becomes implemented, that won't change the fact that $client will only contain the most recently connected client, not all of them.


      We're not surrounded, we're in a target-rich environment!
        jasonk, you are a little bit wrong about Anonymous Monk's code, although his code has problem, but not exactly as you pointed out.

        First play with two pieces od demos I created:
        use threads; use threads::shared; use strict; my $a : shared = 1; { #our $a = 2; print $a; }
        And this one:
        use threads; use threads::shared; use strict; my $a : shared = 1; threads->create(\&display); <STDIN>; sub display { #our $a = 2; print $a; }


        Try to play with that line I commented out, try to comment it and uncomment it, see what you would get.
        1. If you comment out that line (doesn't matter which version of my demo, the one with two threads, or the one with actually one thread), you would get 1, because $a refers to the shared variable, and its value is 1.
        2. If you uncomment that line, you would get 2, because now $a refers to our $a.
        Now let's carefully examine Anonymous Monk's code together. You can see that, before he call accept(), he actually declared $client in the same scope, and passed the newly accepted socket to his start_thread function, so he does stored multiple connections.

        However, in his begin function, he used the shared $client, which is not initialized.

        More important, we should look at this from a conceptual view, not focus on his code too much (his code has some bugs, but that does not mean that what in his mind is wrong.)

        From a conceptual view, as I said in my original reply, to share socket among threads will not be a problem for Perl, and it should be shared just like any other object. Just wait for bless to be fixed.

Re: sharing variables with threads
by Anonymous Monk on Mar 13, 2003 at 05:16 UTC
    Thanks alot pg, your explanations helped me understand how threading actually works, and made it easy to understand.
Re: sharing variables with threads
by jasonk (Parson) on Mar 13, 2003 at 01:49 UTC

    Even if you could share sockets like that (I don't think you can), every client that connected would replace the $client variable, you can't have more than one client connection referred to by the same variable.


    We're not surrounded, we're in a target-rich environment!
      would it work if i shared the $line variable, so that everyone would see what everyone is typing?

        No, you are not going to be able to do it the way you are attempting, you will have to keep a list of client connections, and every time you get a line from one client, go through the list and send it to every other client. This would actually work better without threads, using a server that sets all the clients to be non-blocking.


        We're not surrounded, we're in a target-rich environment!