Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: share socket connections amongst multiple threads?

by BrowserUk (Patriarch)
on Jul 08, 2005 at 20:17 UTC ( [id://473559]=note: print w/replies, xml ) Need Help??


in reply to Re^2: share socket connections amongst multiple threads?
in thread share socket connections amongst multiple threads?

Your explanation of what you are trying to do is still lacking.

Just for a moment, lets assume that you can arrange for multiple threads to have a copy of the same socket. At this point, you have 1 client writing to 1 socket and multiple server threads wanting to read from it. The first thread that reads the socket will get the data, and all the others won't.

You mention "re-broadcasting". How?

Sockets are point to point. In order for any one server thread to be able to re-broadcast data to every other server thread, it would need to have a separate socket connection for each of those other servers and re-transmit the data to all of them.

For 2 server threads, you would need 2 sockets. 1 to the client and 1 between the threads.

For 3 server threads, you would need 3 sockets + the client: s1<->s2; s1<->s3; s2<->s3; + s(n)<->C;

For 4 server threads, you would need 6 sockets + the client: s1<->s2; s1<->s3; s1<->s4; s2<->s3; s2<->s4; s3<->s4;

For 5 server threads, you would need 10 sockets + the client: ...

For 6 server threads, you would need 15 ... I think you can see where this is going.

And, remember we are just assuming that you could successfully share the socket to the client between multiple threads, which you cannot. You would also have to arrange for each thread to "monitor" all of these sockets waiting for input.

Put succinctly, what you are trying to do is not a "limitation of threads", but a limitation of your understanding. A bad design that could never work.

In order for us to suggest solutions, you will need to explain what you are trying to achieve, rather than how you are trying to achieve it.

If the idea is that all threads will have access to data read from a client, then reading that data from the socket on one thread only and then placing it into shared memory such that all clients have access to it is possible--but there is a problem.

  • When will you know that the data has been seen by all the threads that need access to it? That is to say, when will you know that any given piece of data is finished with?

    If you do not have some mechanism for deciding when a piece of data will be discarded, then all inbound data will simply accumulate in memory and you have a memory problem.

    The classic way of dealing with this problem for some applications is a shared queue. This works well for producer-consumer type problems where each piece of data placed on the queue by a producer is consumed by only one consumer.

    But, from the little you have said, I think you are more likely talking about something like an IRC server or MUD server. For this type of application you are better off using one queue per client thread, and a central dispatcher or controller thread with its own queue, plus a listener/client thread factory which may or may not need its own queue depending upon the details of the application.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
  • Replies are listed 'Best First'.
    Re^4: share socket connections amongst multiple threads?
    by Elijah (Hermit) on Jul 09, 2005 at 04:30 UTC
      It seems I have not been clear enough. When the server accept()'s a connection I add the connection id to a hash so I will have a list of connection id's. Now when any data from any connection comes into the server I need the server to turn around and send that data out to all connections. This does work somewhat but the problem is that when the first user connects the thread is spawned and only ever sees itself. When the second one connects the thread is spawned and the id is added to the hash along with the first and now the second sees itself and the first. The third connects and sees itself and 1 and 2. However 1 cannot see 2 or 3 and 2 cannot see 3. This is because I can not share this list/hash with each thread.
      Does this give you a better idea of what I am talking about now?
        When the server accept()'s a connection I add the connection id to a hash ...

        What do you mean by "connection id"?

        My assumption is that you mean an IO::Socket::INET handle. This is a blessed ref:

        use IO::Socket::Inet;; $sock = IO::Socket::INET->new( PeerAddr => 'www.perl.org', PeerPort => 'http(80)', Proto => 't +cp' );; print $sock;; IO::Socket::INET=GLOB(0x1a05ec0)

        which, for very good reason, you cannot put into a shared hash.

        Basically, the methods that are attached to a blessed object like IO::Socket::INET=GLOB(0x1a05ec0) are tied to the thread in which the object handle (a GLOB in this case) is instantiated. So, if you share that object with another thread and that other thread attempts to call a method upon it, it will try to invoke code from the other thread. That cannot work, which is why Perl stops you from trying to do that.

        There are two approached to solving this problem that I am aware of:

      • The first is "tried and tested", and is the method I described above. Each thread has a Thread::Queue associated with it and when it receives a message it posts a copy to each of the other threads and they send it to the client who's thread they are handling.

        This is easier to arrange by having a "controller" thread that also has a queue and each client forwards a copy of data it receives to the contoller via it's queue and it forwards it to each of the other clients.

      • The second is a little harder to explain, and whilst it has worked for me, is not (I believe) an officially sanctioned technique. That of re-blessing the handle into each thread that needs it.

        Again, this works best if a central controller thread is given access to handles from each of the client handles and it reblesses them. The clients would then forward their messages to the controller and it would forward them (directly) to the clients via the reblessed handles. This still leaves the problem of conveying those handles from the clients to the controller. Not insurmountable, but requires some jiggery pokery.

        All in all, the queues method is probably easier.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Node Status?
    node history
    Node Type: note [id://473559]
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others chilling in the Monastery: (8)
    As of 2024-03-28 09:34 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found