📄 semaphore.pm
字号:
package Thread::Semaphore;use Thread qw(cond_wait cond_broadcast);=head1 NAMEThread::Semaphore - thread-safe semaphores=head1 SYNOPSIS use Thread::Semaphore; my $s = new Thread::Semaphore; $s->up; # Also known as the semaphore V -operation. # The guarded section is here $s->down; # Also known as the semaphore P -operation. # The default semaphore value is 1. my $s = new Thread::Semaphore($initial_value); $s->up($up_value); $s->down($up_value);=head1 DESCRIPTIONSemaphores provide a mechanism to regulate access to resources. Semaphores,unlike locks, aren't tied to particular scalars, and so may be used tocontrol access to anything you care to use them for.Semaphores don't limit their values to zero or one, so they can be used tocontrol access to some resource that may have more than one of. (Forexample, filehandles) Increment and decrement amounts aren't fixed at oneeither, so threads can reserve or return multiple resources at once.=head1 FUNCTIONS AND METHODS=over 8=item new=item new NUMBERC<new> creates a new semaphore, and initializes its count to the passednumber. If no number is passed, the semaphore's count is set to one.=item down=item down NUMBERThe C<down> method decreases the semaphore's count by the specified number,or one if no number has been specified. If the semaphore's count would dropbelow zero, this method will block until such time that the semaphore'scount is equal to or larger than the amount you're C<down>ing thesemaphore's count by.=item up=item up NUMBERThe C<up> method increases the semaphore's count by the number specified,or one if no number's been specified. This will unblock any thread blockedtrying to C<down> the semaphore if the C<up> raises the semaphore countabove what the C<down>s are trying to decrement it by.=back=cutsub new { my $class = shift; my $val = @_ ? shift : 1; bless \$val, $class;}sub down : locked : method { my $s = shift; my $inc = @_ ? shift : 1; cond_wait $s until $$s >= $inc; $$s -= $inc;}sub up : locked : method { my $s = shift; my $inc = @_ ? shift : 1; ($$s += $inc) > 0 and cond_broadcast $s;}1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -