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

Monks,
Ideally I'd like to shutdown a thread under control via
join(), as opposed to just ignoring it.
My problem is that it is doing a loop, listening on a
socket in blocking mode, so that if no more new pkts
arrive, it will never read the code that checks for a global
var I'm using to signal it.
Ant ideas if this is possible eg force a signal?
If I have to I can just bypass it, I'd just rather do it
nicely.
Cheers
Chris
  • Comment on Shutdown a thread that is using a blocking socket

Replies are listed 'Best First'.
Re: Shutdown a thread that is using a blocking socket (horse's ear)
by tye (Sage) on Mar 28, 2006 at 08:28 UTC

    Then instead of notifying the thread via a global variable, notify it by sending something to its socket or just close (or shutdown) its socket.

    - tye        

Re: Shutdown a thread that is using a blocking socket
by jesuashok (Curate) on Mar 28, 2006 at 06:50 UTC
    Hi

    You can use alarm function. In that function You can just pass time parameters after which you can raise a signal and handle it in your code.

    My advice is that in Network programming don't use Block mode reading or opening Socket files.
    you can use poll or select function in Network programmings

    "Keep pouring your ideas"
Re: Shutdown a thread that is using a blocking socket
by chrism01 (Friar) on Mar 28, 2006 at 23:03 UTC
    jesuashok,
    Actually, I am using select eg:
    # Activate socket $read_handles = IO::Select->new(); $read_handles->add($cfg::listen_socket); # Main loop: check reads/accepts, check writes, check ready to pro +cess # Loop forever, listening for incoming msgs while (1) { # check for new information on the connections we have # NB: use blocking handles. ($new_read_handles) = IO::Select->select($read_handles, undef, undef +);
    When I said blocking socket, I mean it's declared so that it waits on the socket instead of busy waiting round the above loop, wasting cpu.
    Here's the declaration:
    $cfg::listen_socket = IO::Socket::INET->new(LocalPort => $cfg::params{'RPS_P +ORT'}, Type => SOCK_STREAM, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1, Blocking => 1 ) # Blocking is +on
    As I understand ALRM (which occurred to me), I was going to use it in the ctrl thread to give up waiting for the listen thr to shutdown after X seconds, but it seems to me that's more or less equiv to just ignoring the listen thr anyway, as it (listen thr) won't actually shutdown.

    tye: I suppose I could use the shutdown cmd as long as I make the socket declaration threads::shared::share($cfg::listen_socket). At the moment it's only global within the listen thr.

    Cheers
    Chris
      Interestingly, even though I can't (perl won't let me) threads::shared::share($cfg::listen_socket), (because it's too complex an object), declaring it in the ctrl thread then assigning/using it in the listen thr still allows the shutdown() cmd from the ctrl thr to affect the listen thr, which is what I wanted. :-)

      Cheers
      Chris