I'm trying to set up a ctrl/C handler in conjunction with a blocking read (Linux::Inotify2) within a thread. Neither of the approaches works, i.e. in neither case does the "Sighandler x tripped" ... "Exited while." appear. What do I need to do to get it to "behave"?

Thanks!
#!/usr/bin/perl use Cwd; use Data::Dumper; use Linux::Inotify2; use threads; use Thread::Queue; use Time::HiRes; use strict; use warnings; $|++; my($ToBeDone_q); my(@Worker_a); { $ToBeDone_q=Thread::Queue->new(); push(@Worker_a,threads->new(\&Watchdog,$ToBeDone_q)); }; # Process the queued up signals while (my($Request_s)=$ToBeDone_q->dequeue()) { warn ''.Data::Dumper->Dump([\$Request_s],[qw(*Request_s)]).' ' +; if ($Request_s eq 'All done!') { # Took one "All Done!" off last; } else { # Perform the work if possible my(%_h); eval $Request_s; # Do what you want to do with the results from the watchdo +g ToBeDone(); }; }; grep {$_->join;} @Worker_a; # Just In Case! while ( my(@list)=threads->list()) { print "$#list\n"; grep { $_->join } @list; }; exit; sub Watchdog { my($ToBeDone_q)=@_; print "Starting thread.\n"; my $Inotify_o=Linux::Inotify2->new(); $Inotify_o->watch(Cwd::abs_path('.'),IN_ALL_EVENTS); my $Done_f:shared; # Ctrl/c signal handler 1 - doesn't seem to work #$SIG{INT}=$SIG{TERM}=$SIG{HUP}=sub { # ? # # print "SignalHandler 1 tripped!\n"; # lock $Done_f; # $Done_f=1; # print "Attempting to unblock Inotify\n"; # $Inotify_o->blocking(0); # print "Inotify unblocked!\n" # }; # SignalHandler: Done # Ctrl/c signal handler 2 - doesn't seem to work ... sub _Sighandler { print "SignalHandler 2 tripped!\n"; lock $Done_f; $Done_f=1; print "Attempting to unblock Inotify\n"; $Inotify_o->blocking(0); print "Inotify unblocked!\n" }; # SignalHandler: Done #$SIG{INT}=$SIG{TERM}=$SIG{HUP}=\&_Sighandler; print "Starting while.\n"; while (!$Done_f) { print "Waiting on read.\n"; my @events_ao = $Inotify_o->read; print "read has been unblocked.\n"; unless (@events_ao > 0){ print "read error: $!"; } else { # To Do! foreach my $event_o (@events_ao) { print $event_o->fullname . " was modified\n" if $event +_o->IN_MODIFY; if ($event_o->fullname =~ m{\.pm$} && $event_o->IN_CLO +SE_WRITE) { # Have a module } elsif ($event_o->fullname =~ m{\.(?:cgi|pl)$} && $even +t_o->IN_CLOSE_WRITE) { # Have a script }; }; }; print "Continuing while.\n"; }; print "Exited while.\n"; print "Exiting thread.\n"; return; }; __END__

In reply to Interrupting a blocking read (Linux::Inotify2) with a signal handler within a thread by clueless newbie

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.