Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm using perl 5.8.8 and trying to get the following code runing:
#! /opt/csw/bin/perl use warnings; use strict; use IO::Socket; use threads; sub doWork { my $conn = shift; my $thread = threads->self(); $thread->detach(); $| = 1; print $conn "enter your input: "; chomp(my $userInput = <$conn>); print $conn "got your input : $userInput\n"; $conn->close(); } my $sock = IO::Socket::INET->new(LocalPort => 14000, Listen => 20, ReuseAddr => 1, Proto => 'tcp', ); die "unable to create listening socket: $@" unless($sock); warn "listening...\n"; while(my $conn = $sock->accept()) { threads->create(\&doWork, $conn); }
But I've got the following Problem:

If the thread is detached with $thread->detach();, the loop while(my $conn = $sock->accept()) {...} is left as soon as the new thread has finished it's work. I've trussed the process to check if there are any signals sent when the new thread exists, but could not find any. The while loop keeps running if the thread is not detached.

Does anybody have a glue?

Replies are listed 'Best First'.
Re: accept on listening socket seems affected by threads->detach()
by zentara (Cardinal) on Jul 24, 2008 at 13:00 UTC
      Yes, it works this way.
      I've now tested the code at a Linux machine and perl 5.8.8 . Seems to be something different to blastwave.org 's solaris perl 5.8.8 .

      Thank you for your reply!
Re: accept on listening socket seems affected by threads->detach()
by perreal (Monk) on Jul 24, 2008 at 12:04 UTC
    I tried with v5.8.8 and v5.10.0 on Fedora C9 and it works without leaving the main loop after a detached thread finishes. May be your create thread function does not actually create a thread. you can check with a simple script to verify you actually have two threads running.

    update: typo

      Same result (works fine) with Perl 5.8.8 on SuSE 10.2 with -lpthread. Wonder if that helps...? What does perl -V tell about your Perl installation, esp. threading support?
      Yes, the threads were created (checked $thred->tid).
      I've now tested the code at a Linux machine and perl 5.8.8 . You're right it really works there. Seems to be something different to blastwave.org 's solaris perl 5.8.8 .

      Thank you for your reply!