1 package Thread::Semaphore;
2 use Thread qw(cond_wait cond_broadcast);
6 Thread::Semaphore - thread-safe semaphores
10 use Thread::Semaphore;
11 my $s = new Thread::Semaphore;
12 $s->up; # Also known as the semaphore V -operation.
13 # The guarded section is here
14 $s->down; # Also known as the semaphore P -operation.
16 # The default semaphore value is 1.
17 my $s = new Thread::Semaphore($initial_value);
25 my $val = @_ ? shift : 1;
30 use attrs qw(locked method);
32 my $inc = @_ ? shift : 1;
33 cond_wait $s until $$s >= $inc;
38 use attrs qw(locked method);
40 my $inc = @_ ? shift : 1;
41 ($$s += $inc) > 0 and cond_broadcast $s;