1threads::shared(3pm)   Perl Programmers Reference Guide   threads::shared(3pm)
2
3
4

NAME

6       threads::shared - Perl extension for sharing data structures between
7       threads
8

SYNOPSIS

10         use threads;
11         use threads::shared;
12
13         my $var : shared;
14         $var = $scalar_value;
15         $var = $shared_ref_value;
16         $var = &share($simple_unshared_ref_value);
17         $var = &share(new Foo);
18
19         my($scalar, @array, %hash);
20         share($scalar);
21         share(@array);
22         share(%hash);
23         my $bar = &share([]);
24         $hash{bar} = &share({});
25
26         { lock(%hash); ...  }
27
28         cond_wait($scalar);
29         cond_timedwait($scalar, time() + 30);
30         cond_broadcast(@array);
31         cond_signal(%hash);
32
33         my $lockvar : shared;
34         # condition var != lock var
35         cond_wait($var, $lockvar);
36         cond_timedwait($var, time()+30, $lockvar);
37

DESCRIPTION

39       By default, variables are private to each thread, and each newly cre‐
40       ated thread gets a private copy of each existing variable.  This module
41       allows you to share variables across different threads (and pseudoforks
42       on Win32).  It is used together with the threads module.
43

EXPORT

45       "share", "cond_wait", "cond_timedwait", "cond_signal", "cond_broadcast"
46
47       Note that if this module is imported when "threads" has not yet been
48       loaded, then these functions all become no-ops. This makes it possible
49       to write modules that will work in both threaded and non-threaded envi‐
50       ronments.
51

FUNCTIONS

53       share VARIABLE
54           "share" takes a value and marks it as shared. You can share a
55           scalar, array, hash, scalar ref, array ref or hash ref.  "share"
56           will return the shared rvalue but always as a reference.
57
58           "share" will traverse up references exactly one level.
59           "share(\$a)" is equivalent to "share($a)", while "share(\\$a)" is
60           not.  This means that you must create nested shared data structures
61           by first creating individual shared leaf notes, then adding them to
62           a shared hash or array.
63
64           A variable can also be marked as shared at compile time by using
65           the "shared" attribute: "my $var : shared".
66
67           If you want to share a newly created reference unfortunately you
68           need to use "&share([])" and "&share({})" syntax due to problems
69           with Perl's prototyping.
70
71           The only values that can be assigned to a shared scalar are other
72           scalar values, or shared refs, eg
73
74               my $var : shared;
75               $var = 1;              # ok
76               $var = &share([]);     # ok
77               $var = [];             # error
78               $var = A->new;         # error
79               $var = &share(A->new); # ok as long as the A object is not nested
80
81           Note that it is often not wise to share an object unless the class
82           itself has been written to support sharing; for example, an
83           object's destructor may get called multiple times, one for each
84           thread's scope exit.
85
86       lock VARIABLE
87           "lock" places a lock on a variable until the lock goes out of
88           scope.  If the variable is locked by another thread, the "lock"
89           call will block until it's available. "lock" is recursive, so mul‐
90           tiple calls to "lock" are safe -- the variable will remain locked
91           until the outermost lock on the variable goes out of scope.
92
93           If a container object, such as a hash or array, is locked, all the
94           elements of that container are not locked. For example, if a thread
95           does a "lock @a", any other thread doing a "lock($a[12])" won't
96           block.
97
98           "lock" will traverse up references exactly one level.  "lock(\$a)"
99           is equivalent to "lock($a)", while "lock(\\$a)" is not.
100
101           Note that you cannot explicitly unlock a variable; you can only
102           wait for the lock to go out of scope. If you need more fine-grained
103           control, see Thread::Semaphore.
104
105       cond_wait VARIABLE
106       cond_wait CONDVAR, LOCKVAR
107           The "cond_wait" function takes a locked variable as a parameter,
108           unlocks the variable, and blocks until another thread does a
109           "cond_signal" or "cond_broadcast" for that same locked variable.
110           The variable that "cond_wait" blocked on is relocked after the
111           "cond_wait" is satisfied.  If there are multiple threads
112           "cond_wait"ing on the same variable, all but one will reblock wait‐
113           ing to reacquire the lock on the variable. (So if you're only using
114           "cond_wait" for synchronisation, give up the lock as soon as possi‐
115           ble). The two actions of unlocking the variable and entering the
116           blocked wait state are atomic, the two actions of exiting from the
117           blocked wait state and relocking the variable are not.
118
119           In its second form, "cond_wait" takes a shared, unlocked variable
120           followed by a shared, locked variable.  The second variable is
121           unlocked and thread execution suspended until another thread sig‐
122           nals the first variable.
123
124           It is important to note that the variable can be notified even if
125           no thread "cond_signal" or "cond_broadcast" on the variable.  It is
126           therefore important to check the value of the variable and go back
127           to waiting if the requirement is not fulfilled.  For example, to
128           pause until a shared counter drops to zero:
129
130               { lock($counter); cond_wait($count) until $counter == 0; }
131
132       cond_timedwait VARIABLE, ABS_TIMEOUT
133       cond_timedwait CONDVAR, ABS_TIMEOUT, LOCKVAR
134           In its two-argument form, "cond_timedwait" takes a locked variable
135           and an absolute timeout as parameters, unlocks the variable, and
136           blocks until the timeout is reached or another thread signals the
137           variable.  A false value is returned if the timeout is reached, and
138           a true value otherwise.  In either case, the variable is re-locked
139           upon return.
140
141           Like "cond_wait", this function may take a shared, locked variable
142           as an additional parameter; in this case the first parameter is an
143           unlocked condition variable protected by a distinct lock variable.
144
145           Again like "cond_wait", waking up and reacquiring the lock are not
146           atomic, and you should always check your desired condition after
147           this function returns.  Since the timeout is an absolute value,
148           however, it does not have to be recalculated with each pass:
149
150               lock($var);
151               my $abs = time() + 15;
152               until ($ok = desired_condition($var)) {
153                 last if !cond_timedwait($var, $abs);
154               }
155               # we got it if $ok, otherwise we timed out!
156
157       cond_signal VARIABLE
158           The "cond_signal" function takes a locked variable as a parameter
159           and unblocks one thread that's "cond_wait"ing on that variable. If
160           more than one thread is blocked in a "cond_wait" on that variable,
161           only one (and which one is indeterminate) will be unblocked.
162
163           If there are no threads blocked in a "cond_wait" on the variable,
164           the signal is discarded. By always locking before signaling, you
165           can (with care), avoid signaling before another thread has entered
166           cond_wait().
167
168           "cond_signal" will normally generate a warning if you attempt to
169           use it on an unlocked variable. On the rare occasions where doing
170           this may be sensible, you can skip the warning with
171
172               { no warnings 'threads'; cond_signal($foo) }
173
174       cond_broadcast VARIABLE
175           The "cond_broadcast" function works similarly to "cond_signal".
176           "cond_broadcast", though, will unblock all the threads that are
177           blocked in a "cond_wait" on the locked variable, rather than only
178           one.
179

NOTES

181       threads::shared is designed to disable itself silently if threads are
182       not available. If you want access to threads, you must "use threads"
183       before you "use threads::shared".  threads will emit a warning if you
184       use it after threads::shared.
185

BUGS

187       "bless" is not supported on shared references. In the current version,
188       "bless" will only bless the thread local reference and the blessing
189       will not propagate to the other threads. This is expected to be imple‐
190       mented in a future version of Perl.
191
192       Does not support splice on arrays!
193
194       Taking references to the elements of shared arrays and hashes does not
195       autovivify the elements, and neither does slicing a shared array/hash
196       over non-existent indices/keys autovivify the elements.
197
198       share() allows you to "share $hashref->{key}" without giving any error
199       message.  But the "$hashref->{key}" is not shared, causing the error
200       "locking can only be used on shared values" to occur when you attempt
201       to "lock $hasref->{key}".
202

AUTHOR

204       Arthur Bergman <arthur at contiller.se>
205
206       threads::shared is released under the same license as Perl
207
208       Documentation borrowed from the old Thread.pm
209

SEE ALSO

211       threads, perlthrtut,
212       <http://www.perl.com/pub/a/2002/06/11/threads.html>
213
214
215
216perl v5.8.8                       2001-09-21              threads::shared(3pm)
Impressum