Skip to content

Instantly share code, notes, and snippets.

@yaotti
Created May 11, 2010 16:39
Show Gist options
  • Save yaotti/397526 to your computer and use it in GitHub Desktop.
Save yaotti/397526 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Coro;
use Coro::Semaphore;
sub p { warn shift; }
my $sem = new Coro::Semaphore 1; # 0: locked, 1: unlocked (default)
p sprintf 'semaphores: %d', $sem->count;
async {
p 'unlocking semaphore';
$sem->up;
};
p 'locking semaphore';
$sem->down; # already down
p 'try to get lock';
p($sem->try ? 'succeed' : 'fail');
cede;
p 'try to get lock(2)';
p($sem->try ? 'succeed' : 'fail');
__END__
semaphores: 1 at - line 7.
locking semaphore at - line 7.
try to get lock at - line 7.
fail at - line 7.
unlocking semaphore at - line 7.
try to get lock(2) at - line 7.
succeed at - line 7.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment