You don't show how you intend to use threads, so I'll assume you're going to spawn N worker threads, and pass the sockets to them as needed via a Thread::Queue object.

GLOB's can't be threads::shared, and aren't scalars, so they can't be passed over Thread::Queue (which is just a threads::shared array). However, you *can* get the fileno of the socket after accept()'ing, and pass it to the worker instead: (The following code hasn't been tested, its just an outline)

my $thrdq = Thread::Queue->new(); my @thrds = (); push @thrds, threads->new(\&runworker) foreach (1..$num_of_threads); while( my $newClient = $server->accept ) { $thrdq->enqueue(fileno($newClient)); } sub runworker { my $sktno = $thrdq->dequeue(); my $client = IO::Socket::INET->new(); $client->fdopen($fn, '+>>'); print $client "CLIENT: some stuff\n" }
IIRC, you'll also need to wait for the work thread to pick up the fileno before accepting another connection, or the master may cause a socket close when it accepts the next connection.

In reply to Re: How to share sockets between threads by renodino
in thread How to share sockets between threads by Anonymous Monk

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.