in reply to How to share sockets between threads
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)
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.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" }
|
|---|