http://qs1969.pair.com?node_id=473530


in reply to share socket connections amongst multiple threads?

Why do you need to share the IO::Socket::INET connection object? I dont get it. If you want to send data to all of the threads you dont need the object, just a shared var or two that you can use as a queue. Am I missing what you are trying to do here?


-Waswas

Replies are listed 'Best First'.
Re^2: share socket connections amongst multiple threads?
by Elijah (Hermit) on Jul 08, 2005 at 18:52 UTC
    Am I missing what you are trying to do here?

    I believe so. Each thread is a spawned socket connection between the server and client that is connecting. When each threaded socket gets sent data on it's respective connection it needs to re-broadcast this data to all the other socket connections. I am not trying to share simple text or integer data between the threads, I am trying to make each socket connection aware of the rest.

      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.
        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?
      That I understand, what I don't understand is what that process has to do with the actual socket. It seems like the model should be: start each connection on its own thread -- that thread is in control of the connection. If any data is recived by any thread then assign that to a "queue" (shared var or vars) and then have each of the other threads that are masters of their connection's read off that queue and push out the data on their connection. I just dont get what bennifet sharing the connection object has in that view of things.


      -Waswas