http://qs321.pair.com?node_id=905330

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(); }); }