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
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |