I have the following basic code structure:

a main program with a thread pool accepting tcp connections all data received from these connections are put on a thread queue which another thread handles

the "other" thread: this thread has got an irc-connection (Net::IRC) with an infinite while-loop which first handles irc-stuff (do_one_loop) and secondly dequeues the previous mentioned queue.

All irc-events are forwarded to another (or actually several other) tcp-connections (the brains). These brains are separate programs which handles the events, generate replies and send these back via tcp to the main-part (which will queue and be dequeued by irc-thread and printed to channel or whatever).

Now, this all works fine. The problem I'm having is that I want my program to, in a good fashion, reconnect to any lost brain connection. So that if any brain is restarted, it will be reconnected again. I've set SIGPIPE to ignore and successfully detect deconnected brain-connections. The brain connections are stored in a hash with connection-numbers as keys (0, 1, 2, ...). Once a connection is detected as lost its key (connection number) is put on a reconnect-queue, which a reconnect thread dequeues and then tries to reconnect to this connection. ($brains->{$key}->{Socket} = IO::Socket::INET->new...)

Now, of course, this doesn't work, since this brains-hash will be a separate copy from the one in the irc-thread. Or at least that's how I've understood it. I've tried sending references to the thread too, but it seems to have the same effect.

So my question basically boils down to this: How can I reconnect IO::Socket::INET connections in a separate thread?

I want this to be done in a separate (detached) thread to avoid the irc-thread to get stuck while trying to connect to brains.

Is this even possible? Or should I just set these connection to nonblocking and call it a day?


In reply to sharing (io::socket) objects with threads by bumby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.