I have a *nix program that emulates 'top' in that it continually loops and renders data in a shell window. The program starts, and creates three threads which gather distinct information, update a global shared hash, sleep for some period, and then repeat. The sleep is there because I don't want to hammer the resources.

A fourth thread renders all the shared global data, sleeps and repeats, while the main program loops and performs a non-blocking read to see if a key has been pressed. If 'q' has, then it sets a shared global $quit = 1, then joins the threads and exits.

The effect is one of pretty, blinking lights, that demo well. The program is performing very well, except that when I press 'q', all the threads are looping and sleeping, and only get to check $quit between sleep calls. This means that there is a delay (of up to 10 seconds) until all threads wake up and notice $quit == 1.

As I see it, I could simply exit as soon as I see a 'q' pressed, but that seems untidy. Is there a way to avoid the delay, and interrupt all the sleep calls? Could someone suggest an entirely better way of doing this? (Code simplified for brevity).

#! /usr/bin/perl -w use strict; use threads; use threads::shared; use Term::ReadKey; my $quit : shared = 0; sub gatherData1 { while (!$quit) {... sleep 1} } sub gatherData2 { while (!$quit) {... sleep 5} } sub gatherData3 { while (!$quit) {... sleep 10} } sub renderData { while (!$quit) {... sleep 1} } my $gatherer1 = threads->new (\&gatherData1); my $gatherer2 = threads->new (\&gatherData2); my $gatherer3 = threads->new (\&gatherData3); my $renderer = threads->new (\&renderData); while (!$quit) { ReadMode 'cbreak'; $quit = 1 if defined (my $char = ReadKey (0)) && $char eq 'q'; ReadMode 'normal'; } $gatherer1->join; $gatherer2->join; $gatherer3->join; $renderer->join; exit 0;

In reply to Dormus interruptus by pbeckingham

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.