in reply to Re^4: How do you share an object(Telnet Session) between threads? (blessed glob)
in thread How do you share an object(Telnet Session) between threads?

Oh, I agree with most of what you say. And I'm not surprised that it (at least) often doesn't work. There are plenty of things that can make it go wrong (as I noted). (Hell, I have several experiences of trying to get things to work via Perl threads w/o even trying to share file handles and not being able to get it to work. ;)

But I was not proposing even multiple transactions much less concurrent transactions. It seemed clear to me that what was desired was access to a single session from multiple threads but not concurrently (and that impression is enforced by subsequent comments).

You can have multiple Perl handles to the same file descriptor. And you can arrange for the object state outside of that file handle to be copied to other threads via threads::shared. So, thread X does some operations, E, with the telnet session. That causes some data to be written to or read from the socket and for the state of the object to be changed. Later, some operations, F, are desired. If they happened to be done by thread X, then, of course, they work fine. But (with many caveats), thread Y should also be able to attempt F and it will get an updated copy of the object state and then interact with the same socket file descriptor but via a different Perl file handle and the remote system will see no difference compared to what thread X would have done.

But it is indeed better to arrange for only one thread to have need of the telnet session. I am certainly not advocating against that. I was lazy for not being explicit that the approach I described should be a last resort. I kinda thought that such was not hard to realize, but being clearer would have been better.

I wish I had time to actually try this out and diagnose the types of problems I run into. I'd be surprised if I didn't run into any. Digging into the specifics would be interesting (and often educational).

- tye        

  • Comment on Re^5: How do you share an object(Telnet Session) between threads? (possible?)

Replies are listed 'Best First'.
Re^6: How do you share an object(Telnet Session) between threads? (possible?)
by BrowserUk (Patriarch) on Sep 29, 2015 at 23:17 UTC

      Oh, I'm sure you could. But I don't have any failed attempts handy. Between some minor improvements to Perl, some better tools I've written, some minor improvements to my own skills, and surely a bit about the types of problems that I've taken on of late, I just don't find myself even tempted to reach for threads when writing Perl recently.

      I'm actually extremely close to releasing a batch of these tools that should also work on Windows (almost as well as they do on Linux), in part based on work from you. So you've already helped me. :)

      - tye