In my experience, the best (or easiest) way to handle that under Linux is to make the underlaying socket non-blocking and then use select to wait for input.

Before the select, place a signal handler that would set a flag variable, and after the select, check if that variable has been set. To work around race-conditions, set a timeout in the select call:

# untested: my $stop = 0; $SIG{INT} = sub { $stop = 1 }; while (1) { my $fileno = fileno($linux_notify2->socket); my $v = ''; vec ($v, $fileno, 1) = 1; select($v, undef, undef, 0.1); last if $stop; if (vec($v, $fileno, 1)) { $linux_notify2->poll; ... } }

Another option, is to use one of the event frameworks available as POE or AnyEvent (though, mixing them with threads can be problematic at least).


In reply to Re: Interrupting a blocking read (Linux::Inotify2) with a signal handler within a thread by salva
in thread 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.