saintmike has asked for the wisdom of the Perl Monks concerning the following question:
What's the best way in AnyEvent to protect critical sections? For example, if I have a function critical() that I don't want to be interrupted by a call to interrupt() in the following code, what kind of mutex-like AnyEvent gimmick would I use?
use AnyEvent::Strict; use AnyEvent; my $w = AnyEvent->condvar(); my $critical_timer; my $interrupt_timer; my $in_critical = 0; critical(); interrupt(); $w->recv(); sub critical { print "critical start\n"; $in_critical = 1; $critical_timer = AnyEvent->timer ( after => 1, cb => sub { print "critical done\n"; $in_critical = 0; critical(); }); } sub interrupt { print "interrupt start\n"; if( $in_critical ) { print "D'oh!!! interrupt while in_critical\n"; } $interrupt_timer = AnyEvent->timer ( after => 1, cb => sub { print "interrupt done\n"; interrupt(); }); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: AnyEvent: How to protect critical sections?
by Corion (Patriarch) on May 17, 2011 at 20:25 UTC | |
by saintmike (Vicar) on May 17, 2011 at 20:37 UTC | |
by Corion (Patriarch) on May 17, 2011 at 20:43 UTC | |
by saintmike (Vicar) on May 17, 2011 at 20:56 UTC | |
by Corion (Patriarch) on May 17, 2011 at 21:03 UTC | |
| |
by Anonymous Monk on May 18, 2011 at 20:00 UTC | |
Re: AnyEvent: How to protect critical sections?
by BrowserUk (Patriarch) on May 17, 2011 at 22:25 UTC | |
by Anonymous Monk on May 18, 2011 at 19:50 UTC | |
by Corion (Patriarch) on May 18, 2011 at 20:11 UTC |
Back to
Seekers of Perl Wisdom