1Thread::Semaphore(3pm) Perl Programmers Reference Guide Thread::Semaphore(3pm)
2
3
4
6 Thread::Semaphore - Thread-safe semaphores
7
9 This document describes Thread::Semaphore version 2.09
10
12 use Thread::Semaphore;
13 my $s = Thread::Semaphore->new();
14 $s->down(); # Also known as the semaphore P operation.
15 # The guarded section is here
16 $s->up(); # Also known as the semaphore V operation.
17
18 # The default semaphore value is 1
19 my $s = Thread::Semaphore-new($initial_value);
20 $s->down($down_value);
21 $s->up($up_value);
22
24 Semaphores provide a mechanism to regulate access to resources. Unlike
25 locks, semaphores aren't tied to particular scalars, and so may be used
26 to control access to anything you care to use them for.
27
28 Semaphores don't limit their values to zero and one, so they can be
29 used to control access to some resource that there may be more than one
30 of (e.g., filehandles). Increment and decrement amounts aren't fixed
31 at one either, so threads can reserve or return multiple resources at
32 once.
33
35 ->new()
36 ->new(NUMBER)
37 "new" creates a new semaphore, and initializes its count to the
38 specified number (which must be an integer). If no number is
39 specified, the semaphore's count defaults to 1.
40
41 ->down()
42 ->down(NUMBER)
43 The "down" method decreases the semaphore's count by the
44 specified number (which must be an integer >= 1), or by one if
45 no number is specified.
46
47 If the semaphore's count would drop below zero, this method
48 will block until such time as the semaphore's count is greater
49 than or equal to the amount you're "down"ing the semaphore's
50 count by.
51
52 This is the semaphore "P operation" (the name derives from the
53 Dutch word "pak", which means "capture" -- the semaphore
54 operations were named by the late Dijkstra, who was Dutch).
55
56 ->up()
57 ->up(NUMBER)
58 The "up" method increases the semaphore's count by the number
59 specified (which must be an integer >= 1), or by one if no
60 number is specified.
61
62 This will unblock any thread that is blocked trying to "down"
63 the semaphore if the "up" raises the semaphore's count above
64 the amount that the "down" is trying to decrement it by. For
65 example, if three threads are blocked trying to "down" a
66 semaphore by one, and another thread "up"s the semaphore by
67 two, then two of the blocked threads (which two is
68 indeterminate) will become unblocked.
69
70 This is the semaphore "V operation" (the name derives from the
71 Dutch word "vrij", which means "release").
72
74 Semaphores created by Thread::Semaphore can be used in both threaded
75 and non-threaded applications. This allows you to write modules and
76 packages that potentially make use of semaphores, and that will
77 function in either environment.
78
80 Thread::Semaphore Discussion Forum on CPAN:
81 http://www.cpanforum.com/dist/Thread-Semaphore
82 <http://www.cpanforum.com/dist/Thread-Semaphore>
83
84 Annotated POD for Thread::Semaphore:
85 http://annocpan.org/~JDHEDDEN/Thread-Semaphore-2.09/lib/Thread/Semaphore.pm
86 <http://annocpan.org/~JDHEDDEN/Thread-
87 Semaphore-2.09/lib/Thread/Semaphore.pm>
88
89 Source repository: http://code.google.com/p/thread-semaphore/
90 <http://code.google.com/p/thread-semaphore/>
91
92 threads, threads::shared
93
95 Jerry D. Hedden, <jdhedden AT cpan DOT org>
96
98 This program is free software; you can redistribute it and/or modify it
99 under the same terms as Perl itself.
100
101
102
103perl v5.12.4 2011-06-07 Thread::Semaphore(3pm)