Created
May 11, 2010 16:39
-
-
Save yaotti/397526 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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