in reply to Spining "stick"

The problem with these solutions is that whilst the "busy" spinner is spinning, the only thing that the program is busy doing is spinning the spinner.

Here's one that allows you to do something useful while it spins.

#! perl -slw use strict; use threads qw[ async yield ]; use threads::shared; my $ready : shared = 0; async{ local $|=1; while( !$ready ) { do{ select undef, undef, undef, 0.2; printf "\r ($_)" } for qw[ / - \ | * ]; } print "\rReady"; $ready = 0; }->detach; for ( 1 .. 10 ) { ## Busy, busy, busy sleep 1; } $ready = 1; yield while $ready;

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: Spining "stick"
by chanio (Priest) on Jul 04, 2004 at 04:52 UTC
    This alternative should print a line only if it succeeds in what is doing as job. That way, there is a real significance of the spinning wheel (sort of:). When the job is not succeeding, the wheel should stay calm (panic!).

    use strict; use threads qw[ async yield ]; use threads::shared; my $ready : shared = 0; my $isOk : shared = 0; async{ local $|=1; while( !$ready ) { do{ select undef, undef, undef, 0.2; printf "\r ($_)" if ($isOk) } for qw[ / - \ | * ]; } print "\rReady"; $ready = 0; }->detach; for ( 1 .. 10 ) { ## Busy, busy, busy $isOk=1; sleep 1; $isOk=0; } $isOk=0; $ready = 1; yield while $ready;

    .{\('v')/}
    _`(___)' __________________________