=encoding euc-jp =head1 名前 Thread::Semaphore - スレッドセーフなセマフォ =head1 概要 use Thread::Semaphore; my $s = new Thread::Semaphore; $s->up; # V操作としても知られる # The guarded section is here $s->down; # P操作としても知られる # 規定値は1 my $s = new Thread::Semaphore($initial_value); $s->up($up_value); $s->down($up_value); =head1 説明 Semaphores provide a mechanism to regulate access to resources. Semaphores, unlike locks, aren't tied to particular scalars, and so may be used to control access to anything you care to use them for. セマフォはリソースに対するアクセスを調整するメカニズムを提供する。ロックと違い、セマフォは特定のスカラーに結びついていない。それゆえ、あなたが使用したいと思うどんなものに対しても、アクセス制御に利用できるだろう。 Semaphores don't limit their values to zero or one, so they can be used to control access to some resource that there may be more than one of. (For example, filehandles). Increment and decrement amounts aren't fixed at one either, so threads can reserve or return multiple resources at once. セマフォはその値をゼロか1かに制限しない。だから一つ以上あるようなリソース(例えばファイルハンドル)に対するアクセス制御に使うことが出来る。増加減少の値は1に固定されてもいないので、スレッドは同時に複数のリソースを確保したり返したりすることができる。 =head1 関数とメソッド =over 8 =item new =item new NUMBER C creates a new semaphore, and initializes its count to the passed number. If no number is passed, the semaphore's count is set to one. Cは新しいセマフォを生成し、与えられた数にカウンタを初期化する。数を与えなければ、セマフォのカウンタには1がセットされる。 =item down =item down NUMBER The C method decreases the semaphore's count by the specified number, or by one if no number has been specified. If the semaphore's count would drop below zero, this method will block until such time that the semaphore's count is equal to or larger than the amount you're Cing the semaphore's count by. Cメソッドはセマフォのカウンタを指定された数だけ減少させる。数が指定されていなければ1減少させる。もしセマフォのカウンタが0より小さくなるなら、このメソッドはカウンタがCで減少させる分と同じかそれ以上の値になるまでブロックする。 =item up =item up NUMBER The C method increases the semaphore's count by the number specified, or by one if no number has been specified. This will unblock any thread blocked trying to C the semaphore if the C raises the semaphore count above the amount that the Cs are trying to decrement it by. Cメソッドはセマフォのカウンタを指定された数だけ増加させる。数が指定されていなければ1増加させる。もしCがセマフォのカウンタをCが減少させる値より大きくするなら、Cを試みてブロック状態になっているスレッドはブロックが解除される。 =back