I would like to "stop" a timer when I want to - from within the callback.
Something like this:
my $timer;
$timer = POSIX::RT::Timer->new(
value => 0.1,
interval => 1 / 90, # 90hz
callback => sub {
# Do stuff, but only until we are tired of it...
$timer->stop() if $counter++ >= 90;
}
);
Any chance for the addition of a "stop" method?
Subject: | timer.pl |
use threads;
use POSIX::RT::Timer;
use POSIX 'pause';
my $counter = 1;
async {
my $timer;
$timer = POSIX::RT::Timer->new(
value => 0.1,
interval => 1 / 90, # 90 times per second,
callback => sub {
warn "Hello, World!: $counter\n";
$timer->stop() if $counter++ >= 90;
}
);
};
pause() while $counter < 90;
$_->join foreach threads->list;