Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Can I/O operations on the same IO::Socket be executed in different threads?

by sundialsvc4 (Abbot)
on May 12, 2015 at 21:25 UTC ( [id://1126465]=note: print w/replies, xml ) Need Help??


in reply to Re: Can I/O operations on the same IO::Socket be executed in different threads?
in thread Can I/O operations on the same IO::Socket be executed in different threads?

I, too, don’t think that I fully understand the basis from which you speak, and I candidly suspect that your example might produce a “false positive” since it waits three seconds.   Thus, it is probably not a representative example of what might go wrong, since in your example it is unlikely that the threads would ever the threads would never actually conflict with one another.

When you’re dealing with a real-world remote system, the status of that system is only represented by the single stream of messages that the system is sending-out on each of its sockets.   I think that, therefore, you need to have one thread on your end which is responsible for knowing the status of every one of them.   And, because socket-operations are not terribly fast, a select() loop works perfectly.   One thread, processing “one message at a time” from all sources at once, can always keep up and will never present a bottleneck.

This venerable strategy elegantly avoids all timing problems.   One thread can easily accommodate all of the connections, and can (centrally) keep track of the status of all of them, so that all of the clients get exactly what they need and want:   “Please send this message to somebody, and give me the reply as soon as you get it.”   Due to the realities of network-I/O (and, remote systems ...), there’s no reason to make things any more complicated, and, as it were, plenty of reasons not to.

Replies are listed 'Best First'.
Re^3: Can I/O operations on the same IO::Socket be executed in different threads?
by ikegami (Patriarch) on May 13, 2015 at 03:09 UTC

    I, too, don’t think that I fully understand the basis from which you speak,

    I figured I'd actually answer the OP's question. You see, the OP asked if one thread could read from a socket while another writes to the same socket.

    BrowserUk's incredible hostility is completely unwarranted. There are common applications for what the OP asks. Tunnels, for one. Another would be to avoid using select+ioctl to do non-blocking output (which probably doesn't work on Windows). I think IPC::Run does exactly this in some circumstances.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-24 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found