| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "threads::shared 3perl" |
| 58 |
.TH threads::shared 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
threads::shared \- Perl extension for sharing data structures between threads |
| 65 |
.SH VERSION |
| 66 |
.IX Header "VERSION" |
| 67 |
This document describes threads::shared version 1.68 |
| 68 |
.SH SYNOPSIS |
| 69 |
.IX Header "SYNOPSIS" |
| 70 |
.Vb 2 |
| 71 |
\& use threads; |
| 72 |
\& use threads::shared; |
| 73 |
\& |
| 74 |
\& my $var :shared; |
| 75 |
\& my %hsh :shared; |
| 76 |
\& my @ary :shared; |
| 77 |
\& |
| 78 |
\& my ($scalar, @array, %hash); |
| 79 |
\& share($scalar); |
| 80 |
\& share(@array); |
| 81 |
\& share(%hash); |
| 82 |
\& |
| 83 |
\& $var = $scalar_value; |
| 84 |
\& $var = $shared_ref_value; |
| 85 |
\& $var = shared_clone($non_shared_ref_value); |
| 86 |
\& $var = shared_clone({\*(Aqfoo\*(Aq => [qw/foo bar baz/]}); |
| 87 |
\& |
| 88 |
\& $hsh{\*(Aqfoo\*(Aq} = $scalar_value; |
| 89 |
\& $hsh{\*(Aqbar\*(Aq} = $shared_ref_value; |
| 90 |
\& $hsh{\*(Aqbaz\*(Aq} = shared_clone($non_shared_ref_value); |
| 91 |
\& $hsh{\*(Aqquz\*(Aq} = shared_clone([1..3]); |
| 92 |
\& |
| 93 |
\& $ary[0] = $scalar_value; |
| 94 |
\& $ary[1] = $shared_ref_value; |
| 95 |
\& $ary[2] = shared_clone($non_shared_ref_value); |
| 96 |
\& $ary[3] = shared_clone([ {}, [] ]); |
| 97 |
\& |
| 98 |
\& { lock(%hash); ... } |
| 99 |
\& |
| 100 |
\& cond_wait($scalar); |
| 101 |
\& cond_timedwait($scalar, time() + 30); |
| 102 |
\& cond_broadcast(@array); |
| 103 |
\& cond_signal(%hash); |
| 104 |
\& |
| 105 |
\& my $lockvar :shared; |
| 106 |
\& # condition var != lock var |
| 107 |
\& cond_wait($var, $lockvar); |
| 108 |
\& cond_timedwait($var, time()+30, $lockvar); |
| 109 |
.Ve |
| 110 |
.SH DESCRIPTION |
| 111 |
.IX Header "DESCRIPTION" |
| 112 |
By default, variables are private to each thread, and each newly created |
| 113 |
thread gets a private copy of each existing variable. This module allows you |
| 114 |
to share variables across different threads (and pseudo-forks on Win32). It |
| 115 |
is used together with the threads module. |
| 116 |
.PP |
| 117 |
This module supports the sharing of the following data types only: scalars |
| 118 |
and scalar refs, arrays and array refs, and hashes and hash refs. |
| 119 |
.SH EXPORT |
| 120 |
.IX Header "EXPORT" |
| 121 |
The following functions are exported by this module: \f(CW\*(C`share\*(C'\fR, |
| 122 |
\&\f(CW\*(C`shared_clone\*(C'\fR, \f(CW\*(C`is_shared\*(C'\fR, \f(CW\*(C`cond_wait\*(C'\fR, \f(CW\*(C`cond_timedwait\*(C'\fR, \f(CW\*(C`cond_signal\*(C'\fR |
| 123 |
and \f(CW\*(C`cond_broadcast\*(C'\fR |
| 124 |
.PP |
| 125 |
Note that if this module is imported when threads has not yet been loaded, |
| 126 |
then these functions all become no-ops. This makes it possible to write |
| 127 |
modules that will work in both threaded and non-threaded environments. |
| 128 |
.SH FUNCTIONS |
| 129 |
.IX Header "FUNCTIONS" |
| 130 |
.IP "share VARIABLE" 4 |
| 131 |
.IX Item "share VARIABLE" |
| 132 |
\&\f(CW\*(C`share\*(C'\fR takes a variable and marks it as shared: |
| 133 |
.Sp |
| 134 |
.Vb 4 |
| 135 |
\& my ($scalar, @array, %hash); |
| 136 |
\& share($scalar); |
| 137 |
\& share(@array); |
| 138 |
\& share(%hash); |
| 139 |
.Ve |
| 140 |
.Sp |
| 141 |
\&\f(CW\*(C`share\*(C'\fR will return the shared rvalue, but always as a reference. |
| 142 |
.Sp |
| 143 |
Variables can also be marked as shared at compile time by using the |
| 144 |
\&\f(CW\*(C`:shared\*(C'\fR attribute: |
| 145 |
.Sp |
| 146 |
.Vb 1 |
| 147 |
\& my ($var, %hash, @array) :shared; |
| 148 |
.Ve |
| 149 |
.Sp |
| 150 |
Shared variables can only store scalars, refs of shared variables, or |
| 151 |
refs of shared data (discussed in next section): |
| 152 |
.Sp |
| 153 |
.Vb 2 |
| 154 |
\& my ($var, %hash, @array) :shared; |
| 155 |
\& my $bork; |
| 156 |
\& |
| 157 |
\& # Storing scalars |
| 158 |
\& $var = 1; |
| 159 |
\& $hash{\*(Aqfoo\*(Aq} = \*(Aqbar\*(Aq; |
| 160 |
\& $array[0] = 1.5; |
| 161 |
\& |
| 162 |
\& # Storing shared refs |
| 163 |
\& $var = \e%hash; |
| 164 |
\& $hash{\*(Aqary\*(Aq} = \e@array; |
| 165 |
\& $array[1] = \e$var; |
| 166 |
\& |
| 167 |
\& # The following are errors: |
| 168 |
\& # $var = \e$bork; # ref of non\-shared variable |
| 169 |
\& # $hash{\*(Aqbork\*(Aq} = []; # non\-shared array ref |
| 170 |
\& # push(@array, { \*(Aqx\*(Aq => 1 }); # non\-shared hash ref |
| 171 |
.Ve |
| 172 |
.IP "shared_clone REF" 4 |
| 173 |
.IX Item "shared_clone REF" |
| 174 |
\&\f(CW\*(C`shared_clone\*(C'\fR takes a reference, and returns a shared version of its |
| 175 |
argument, performing a deep copy on any non-shared elements. Any shared |
| 176 |
elements in the argument are used as is (i.e., they are not cloned). |
| 177 |
.Sp |
| 178 |
.Vb 1 |
| 179 |
\& my $cpy = shared_clone({\*(Aqfoo\*(Aq => [qw/foo bar baz/]}); |
| 180 |
.Ve |
| 181 |
.Sp |
| 182 |
Object status (i.e., the class an object is blessed into) is also cloned. |
| 183 |
.Sp |
| 184 |
.Vb 4 |
| 185 |
\& my $obj = {\*(Aqfoo\*(Aq => [qw/foo bar baz/]}; |
| 186 |
\& bless($obj, \*(AqFoo\*(Aq); |
| 187 |
\& my $cpy = shared_clone($obj); |
| 188 |
\& print(ref($cpy), "\en"); # Outputs \*(AqFoo\*(Aq |
| 189 |
.Ve |
| 190 |
.Sp |
| 191 |
For cloning empty array or hash refs, the following may also be used: |
| 192 |
.Sp |
| 193 |
.Vb 2 |
| 194 |
\& $var = &share([]); # Same as $var = shared_clone([]); |
| 195 |
\& $var = &share({}); # Same as $var = shared_clone({}); |
| 196 |
.Ve |
| 197 |
.Sp |
| 198 |
Not all Perl data types can be cloned (e.g., globs, code refs). By default, |
| 199 |
\&\f(CW\*(C`shared_clone\*(C'\fR will croak if it encounters such items. To change |
| 200 |
this behaviour to a warning, then set the following: |
| 201 |
.Sp |
| 202 |
.Vb 1 |
| 203 |
\& $threads::shared::clone_warn = 1; |
| 204 |
.Ve |
| 205 |
.Sp |
| 206 |
In this case, \f(CW\*(C`undef\*(C'\fR will be substituted for the item to be cloned. If |
| 207 |
set to zero: |
| 208 |
.Sp |
| 209 |
.Vb 1 |
| 210 |
\& $threads::shared::clone_warn = 0; |
| 211 |
.Ve |
| 212 |
.Sp |
| 213 |
then the \f(CW\*(C`undef\*(C'\fR substitution will be performed silently. |
| 214 |
.IP "is_shared VARIABLE" 4 |
| 215 |
.IX Item "is_shared VARIABLE" |
| 216 |
\&\f(CW\*(C`is_shared\*(C'\fR checks if the specified variable is shared or not. If shared, |
| 217 |
returns the variable's internal ID (similar to |
| 218 |
\&\f(CWrefaddr()\fR (see Scalar::Util). Otherwise, returns \f(CW\*(C`undef\*(C'\fR. |
| 219 |
.Sp |
| 220 |
.Vb 5 |
| 221 |
\& if (is_shared($var)) { |
| 222 |
\& print("\e$var is shared\en"); |
| 223 |
\& } else { |
| 224 |
\& print("\e$var is not shared\en"); |
| 225 |
\& } |
| 226 |
.Ve |
| 227 |
.Sp |
| 228 |
When used on an element of an array or hash, \f(CW\*(C`is_shared\*(C'\fR checks if the |
| 229 |
specified element belongs to a shared array or hash. (It does not check |
| 230 |
the contents of that element.) |
| 231 |
.Sp |
| 232 |
.Vb 4 |
| 233 |
\& my %hash :shared; |
| 234 |
\& if (is_shared(%hash)) { |
| 235 |
\& print("\e%hash is shared\en"); |
| 236 |
\& } |
| 237 |
\& |
| 238 |
\& $hash{\*(Aqelem\*(Aq} = 1; |
| 239 |
\& if (is_shared($hash{\*(Aqelem\*(Aq})) { |
| 240 |
\& print("\e$hash{\*(Aqelem\*(Aq} is in a shared hash\en"); |
| 241 |
\& } |
| 242 |
.Ve |
| 243 |
.IP "lock VARIABLE" 4 |
| 244 |
.IX Item "lock VARIABLE" |
| 245 |
\&\f(CW\*(C`lock\*(C'\fR places a \fBadvisory\fR lock on a variable until the lock goes out of |
| 246 |
scope. If the variable is locked by another thread, the \f(CW\*(C`lock\*(C'\fR call will |
| 247 |
block until it's available. Multiple calls to \f(CW\*(C`lock\*(C'\fR by the same thread from |
| 248 |
within dynamically nested scopes are safe \-\- the variable will remain locked |
| 249 |
until the outermost lock on the variable goes out of scope. |
| 250 |
.Sp |
| 251 |
\&\f(CW\*(C`lock\*(C'\fR follows references exactly \fIone\fR level: |
| 252 |
.Sp |
| 253 |
.Vb 3 |
| 254 |
\& my %hash :shared; |
| 255 |
\& my $ref = \e%hash; |
| 256 |
\& lock($ref); # This is equivalent to lock(%hash) |
| 257 |
.Ve |
| 258 |
.Sp |
| 259 |
Note that you cannot explicitly unlock a variable; you can only wait for the |
| 260 |
lock to go out of scope. This is most easily accomplished by locking the |
| 261 |
variable inside a block. |
| 262 |
.Sp |
| 263 |
.Vb 7 |
| 264 |
\& my $var :shared; |
| 265 |
\& { |
| 266 |
\& lock($var); |
| 267 |
\& # $var is locked from here to the end of the block |
| 268 |
\& ... |
| 269 |
\& } |
| 270 |
\& # $var is now unlocked |
| 271 |
.Ve |
| 272 |
.Sp |
| 273 |
As locks are advisory, they do not prevent data access or modification by |
| 274 |
another thread that does not itself attempt to obtain a lock on the variable. |
| 275 |
.Sp |
| 276 |
You cannot lock the individual elements of a container variable: |
| 277 |
.Sp |
| 278 |
.Vb 4 |
| 279 |
\& my %hash :shared; |
| 280 |
\& $hash{\*(Aqfoo\*(Aq} = \*(Aqbar\*(Aq; |
| 281 |
\& #lock($hash{\*(Aqfoo\*(Aq}); # Error |
| 282 |
\& lock(%hash); # Works |
| 283 |
.Ve |
| 284 |
.Sp |
| 285 |
If you need more fine-grained control over shared variable access, see |
| 286 |
Thread::Semaphore. |
| 287 |
.IP "cond_wait VARIABLE" 4 |
| 288 |
.IX Item "cond_wait VARIABLE" |
| 289 |
.PD 0 |
| 290 |
.IP "cond_wait CONDVAR, LOCKVAR" 4 |
| 291 |
.IX Item "cond_wait CONDVAR, LOCKVAR" |
| 292 |
.PD |
| 293 |
The \f(CW\*(C`cond_wait\*(C'\fR function takes a \fBlocked\fR variable as a parameter, unlocks |
| 294 |
the variable, and blocks until another thread does a \f(CW\*(C`cond_signal\*(C'\fR or |
| 295 |
\&\f(CW\*(C`cond_broadcast\*(C'\fR for that same locked variable. The variable that |
| 296 |
\&\f(CW\*(C`cond_wait\*(C'\fR blocked on is re-locked after the \f(CW\*(C`cond_wait\*(C'\fR is satisfied. If |
| 297 |
there are multiple threads \f(CW\*(C`cond_wait\*(C'\fRing on the same variable, all but one |
| 298 |
will re-block waiting to reacquire the |
| 299 |
lock on the variable. (So if you're only |
| 300 |
using \f(CW\*(C`cond_wait\*(C'\fR for synchronization, give up the lock as soon as possible). |
| 301 |
The two actions of unlocking the variable and entering the blocked wait state |
| 302 |
are atomic, the two actions of exiting from the blocked wait state and |
| 303 |
re-locking the variable are not. |
| 304 |
.Sp |
| 305 |
In its second form, \f(CW\*(C`cond_wait\*(C'\fR takes a shared, \fBunlocked\fR variable followed |
| 306 |
by a shared, \fBlocked\fR variable. The second variable is unlocked and thread |
| 307 |
execution suspended until another thread signals the first variable. |
| 308 |
.Sp |
| 309 |
It is important to note that the variable can be notified even if no thread |
| 310 |
\&\f(CW\*(C`cond_signal\*(C'\fR or \f(CW\*(C`cond_broadcast\*(C'\fR on the variable. It is therefore |
| 311 |
important to check the value of the variable and go back to waiting if the |
| 312 |
requirement is not fulfilled. For example, to pause until a shared counter |
| 313 |
drops to zero: |
| 314 |
.Sp |
| 315 |
.Vb 1 |
| 316 |
\& { lock($counter); cond_wait($counter) until $counter == 0; } |
| 317 |
.Ve |
| 318 |
.IP "cond_timedwait VARIABLE, ABS_TIMEOUT" 4 |
| 319 |
.IX Item "cond_timedwait VARIABLE, ABS_TIMEOUT" |
| 320 |
.PD 0 |
| 321 |
.IP "cond_timedwait CONDVAR, ABS_TIMEOUT, LOCKVAR" 4 |
| 322 |
.IX Item "cond_timedwait CONDVAR, ABS_TIMEOUT, LOCKVAR" |
| 323 |
.PD |
| 324 |
In its two-argument form, \f(CW\*(C`cond_timedwait\*(C'\fR takes a \fBlocked\fR variable and an |
| 325 |
absolute timeout in \fIepoch\fR seconds (see \fBtime()\fR in perlfunc |
| 326 |
for more) as parameters, unlocks the variable, and blocks until the |
| 327 |
timeout is reached or another thread signals the variable. A false value is |
| 328 |
returned if the timeout is reached, and a true value otherwise. In either |
| 329 |
case, the variable is re-locked upon return. |
| 330 |
.Sp |
| 331 |
Like \f(CW\*(C`cond_wait\*(C'\fR, this function may take a shared, \fBlocked\fR variable as an |
| 332 |
additional parameter; in this case the first parameter is an \fBunlocked\fR |
| 333 |
condition variable protected by a distinct lock variable. |
| 334 |
.Sp |
| 335 |
Again like \f(CW\*(C`cond_wait\*(C'\fR, waking up and reacquiring the lock are not atomic, |
| 336 |
and you should always check your desired condition after this function |
| 337 |
returns. Since the timeout is an absolute value, however, it does not have to |
| 338 |
be recalculated with each pass: |
| 339 |
.Sp |
| 340 |
.Vb 6 |
| 341 |
\& lock($var); |
| 342 |
\& my $abs = time() + 15; |
| 343 |
\& until ($ok = desired_condition($var)) { |
| 344 |
\& last if !cond_timedwait($var, $abs); |
| 345 |
\& } |
| 346 |
\& # we got it if $ok, otherwise we timed out! |
| 347 |
.Ve |
| 348 |
.IP "cond_signal VARIABLE" 4 |
| 349 |
.IX Item "cond_signal VARIABLE" |
| 350 |
The \f(CW\*(C`cond_signal\*(C'\fR function takes a \fBlocked\fR variable as a parameter and |
| 351 |
unblocks one thread that's \f(CW\*(C`cond_wait\*(C'\fRing |
| 352 |
on that variable. If more than one |
| 353 |
thread is blocked in a \f(CW\*(C`cond_wait\*(C'\fR on that variable, only one (and which one |
| 354 |
is indeterminate) will be unblocked. |
| 355 |
.Sp |
| 356 |
If there are no threads blocked in a \f(CW\*(C`cond_wait\*(C'\fR on the variable, the signal |
| 357 |
is discarded. By always locking before |
| 358 |
signaling, you can (with care), avoid |
| 359 |
signaling before another thread has entered \fBcond_wait()\fR. |
| 360 |
.Sp |
| 361 |
\&\f(CW\*(C`cond_signal\*(C'\fR will normally generate a warning if you attempt to use it on an |
| 362 |
unlocked variable. On the rare occasions |
| 363 |
where doing this may be sensible, you |
| 364 |
can suppress the warning with: |
| 365 |
.Sp |
| 366 |
.Vb 1 |
| 367 |
\& { no warnings \*(Aqthreads\*(Aq; cond_signal($foo); } |
| 368 |
.Ve |
| 369 |
.IP "cond_broadcast VARIABLE" 4 |
| 370 |
.IX Item "cond_broadcast VARIABLE" |
| 371 |
The \f(CW\*(C`cond_broadcast\*(C'\fR function works similarly to \f(CW\*(C`cond_signal\*(C'\fR. |
| 372 |
\&\f(CW\*(C`cond_broadcast\*(C'\fR, though, will unblock \fBall\fR the threads that are blocked in |
| 373 |
a \f(CW\*(C`cond_wait\*(C'\fR on the locked variable, rather than only one. |
| 374 |
.SH OBJECTS |
| 375 |
.IX Header "OBJECTS" |
| 376 |
threads::shared exports a version of \fBbless()\fR that |
| 377 |
works on shared objects such that \fIblessings\fR propagate across threads. |
| 378 |
.PP |
| 379 |
.Vb 3 |
| 380 |
\& # Create a shared \*(AqFoo\*(Aq object |
| 381 |
\& my $foo :shared = shared_clone({}); |
| 382 |
\& bless($foo, \*(AqFoo\*(Aq); |
| 383 |
\& |
| 384 |
\& # Create a shared \*(AqBar\*(Aq object |
| 385 |
\& my $bar :shared = shared_clone({}); |
| 386 |
\& bless($bar, \*(AqBar\*(Aq); |
| 387 |
\& |
| 388 |
\& # Put \*(Aqbar\*(Aq inside \*(Aqfoo\*(Aq |
| 389 |
\& $foo\->{\*(Aqbar\*(Aq} = $bar; |
| 390 |
\& |
| 391 |
\& # Rebless the objects via a thread |
| 392 |
\& threads\->create(sub { |
| 393 |
\& # Rebless the outer object |
| 394 |
\& bless($foo, \*(AqYin\*(Aq); |
| 395 |
\& |
| 396 |
\& # Cannot directly rebless the inner object |
| 397 |
\& #bless($foo\->{\*(Aqbar\*(Aq}, \*(AqYang\*(Aq); |
| 398 |
\& |
| 399 |
\& # Retrieve and rebless the inner object |
| 400 |
\& my $obj = $foo\->{\*(Aqbar\*(Aq}; |
| 401 |
\& bless($obj, \*(AqYang\*(Aq); |
| 402 |
\& $foo\->{\*(Aqbar\*(Aq} = $obj; |
| 403 |
\& |
| 404 |
\& })\->join(); |
| 405 |
\& |
| 406 |
\& print(ref($foo), "\en"); # Prints \*(AqYin\*(Aq |
| 407 |
\& print(ref($foo\->{\*(Aqbar\*(Aq}), "\en"); # Prints \*(AqYang\*(Aq |
| 408 |
\& print(ref($bar), "\en"); # Also prints \*(AqYang\*(Aq |
| 409 |
.Ve |
| 410 |
.SH NOTES |
| 411 |
.IX Header "NOTES" |
| 412 |
threads::shared is designed to disable itself silently if threads are not |
| 413 |
available. This allows you to write modules and packages that can be used |
| 414 |
in both threaded and non-threaded applications. |
| 415 |
.PP |
| 416 |
If you want access to threads, you must \f(CW\*(C`use threads\*(C'\fR before you |
| 417 |
\&\f(CW\*(C`use threads::shared\*(C'\fR. threads will emit a warning if you use it after |
| 418 |
threads::shared. |
| 419 |
.SH WARNINGS |
| 420 |
.IX Header "WARNINGS" |
| 421 |
.IP "\fBcond_broadcast()\fR called on unlocked variable" 4 |
| 422 |
.IX Item "cond_broadcast() called on unlocked variable" |
| 423 |
.PD 0 |
| 424 |
.IP "\fBcond_signal()\fR called on unlocked variable" 4 |
| 425 |
.IX Item "cond_signal() called on unlocked variable" |
| 426 |
.PD |
| 427 |
See "cond_signal VARIABLE", above. |
| 428 |
.SH "BUGS AND LIMITATIONS" |
| 429 |
.IX Header "BUGS AND LIMITATIONS" |
| 430 |
When \f(CW\*(C`share\*(C'\fR is used on arrays, hashes, array refs or hash refs, any data |
| 431 |
they contain will be lost. |
| 432 |
.PP |
| 433 |
.Vb 3 |
| 434 |
\& my @arr = qw(foo bar baz); |
| 435 |
\& share(@arr); |
| 436 |
\& # @arr is now empty (i.e., == ()); |
| 437 |
\& |
| 438 |
\& # Create a \*(Aqfoo\*(Aq object |
| 439 |
\& my $foo = { \*(Aqdata\*(Aq => 99 }; |
| 440 |
\& bless($foo, \*(Aqfoo\*(Aq); |
| 441 |
\& |
| 442 |
\& # Share the object |
| 443 |
\& share($foo); # Contents are now wiped out |
| 444 |
\& print("ERROR: \e$foo is empty\en") |
| 445 |
\& if (! exists($foo\->{\*(Aqdata\*(Aq})); |
| 446 |
.Ve |
| 447 |
.PP |
| 448 |
Therefore, populate such variables \fBafter\fR declaring them as shared. (Scalar |
| 449 |
and scalar refs are not affected by this problem.) |
| 450 |
.PP |
| 451 |
Blessing a shared item after it has been nested in another shared item does |
| 452 |
not propagate the blessing to the shared reference: |
| 453 |
.PP |
| 454 |
.Vb 5 |
| 455 |
\& my $foo = &share({}); |
| 456 |
\& my $bar = &share({}); |
| 457 |
\& $bar\->{foo} = $foo; |
| 458 |
\& bless($foo, \*(Aqbaz\*(Aq); # $foo is now of class \*(Aqbaz\*(Aq, |
| 459 |
\& # but $bar\->{foo} is unblessed. |
| 460 |
.Ve |
| 461 |
.PP |
| 462 |
Therefore, you should bless objects before sharing them. |
| 463 |
.PP |
| 464 |
It is often not wise to share an object unless the class itself has been |
| 465 |
written to support sharing. For example, a shared object's destructor may |
| 466 |
get called multiple times, once for each thread's scope exit, or may not |
| 467 |
get called at all if it is embedded inside another shared object. Another |
| 468 |
issue is that the contents of hash-based objects will be lost due to the |
| 469 |
above mentioned limitation. See \fIexamples/class.pl\fR (in the CPAN |
| 470 |
distribution of this module) for how to create a class that supports object |
| 471 |
sharing. |
| 472 |
.PP |
| 473 |
Destructors may not be called on objects if those objects still exist at |
| 474 |
global destruction time. If the destructors must be called, make sure |
| 475 |
there are no circular references and that nothing is referencing the |
| 476 |
objects before the program ends. |
| 477 |
.PP |
| 478 |
Does not support \f(CW\*(C`splice\*(C'\fR on arrays. Does not support explicitly changing |
| 479 |
array lengths via $#array \-\- use \f(CW\*(C`push\*(C'\fR and \f(CW\*(C`pop\*(C'\fR instead. |
| 480 |
.PP |
| 481 |
Taking references to the elements of shared arrays and hashes does not |
| 482 |
autovivify the elements, and neither does slicing a shared array/hash over |
| 483 |
non-existent indices/keys autovivify the elements. |
| 484 |
.PP |
| 485 |
\&\f(CWshare()\fR allows you to \f(CWshare($hashref\->{key})\fR and |
| 486 |
\&\f(CWshare($arrayref\->[idx])\fR without giving any error message. But the |
| 487 |
\&\f(CW\*(C`$hashref\->{key}\*(C'\fR or \f(CW\*(C`$arrayref\->[idx]\*(C'\fR is \fBnot\fR shared, causing |
| 488 |
the error "lock can only be used on shared values" to occur when you attempt |
| 489 |
to \f(CWlock($hashref\->{key})\fR or \f(CWlock($arrayref\->[idx])\fR in another |
| 490 |
thread. |
| 491 |
.PP |
| 492 |
Using \f(CWrefaddr()\fR is unreliable for testing |
| 493 |
whether or not two shared references are equivalent (e.g., when testing for |
| 494 |
circular references). Use \fBis_shared()\fR, instead: |
| 495 |
.PP |
| 496 |
.Vb 3 |
| 497 |
\& use threads; |
| 498 |
\& use threads::shared; |
| 499 |
\& use Scalar::Util qw(refaddr); |
| 500 |
\& |
| 501 |
\& # If ref is shared, use threads::shared\*(Aqs internal ID. |
| 502 |
\& # Otherwise, use refaddr(). |
| 503 |
\& my $addr1 = is_shared($ref1) || refaddr($ref1); |
| 504 |
\& my $addr2 = is_shared($ref2) || refaddr($ref2); |
| 505 |
\& |
| 506 |
\& if ($addr1 == $addr2) { |
| 507 |
\& # The refs are equivalent |
| 508 |
\& } |
| 509 |
.Ve |
| 510 |
.PP |
| 511 |
\&\fBeach()\fR does not work properly on shared references |
| 512 |
embedded in shared structures. For example: |
| 513 |
.PP |
| 514 |
.Vb 2 |
| 515 |
\& my %foo :shared; |
| 516 |
\& $foo{\*(Aqbar\*(Aq} = shared_clone({\*(Aqa\*(Aq=>\*(Aqx\*(Aq, \*(Aqb\*(Aq=>\*(Aqy\*(Aq, \*(Aqc\*(Aq=>\*(Aqz\*(Aq}); |
| 517 |
\& |
| 518 |
\& while (my ($key, $val) = each(%{$foo{\*(Aqbar\*(Aq}})) { |
| 519 |
\& ... |
| 520 |
\& } |
| 521 |
.Ve |
| 522 |
.PP |
| 523 |
Either of the following will work instead: |
| 524 |
.PP |
| 525 |
.Vb 4 |
| 526 |
\& my $ref = $foo{\*(Aqbar\*(Aq}; |
| 527 |
\& while (my ($key, $val) = each(%{$ref})) { |
| 528 |
\& ... |
| 529 |
\& } |
| 530 |
\& |
| 531 |
\& foreach my $key (keys(%{$foo{\*(Aqbar\*(Aq}})) { |
| 532 |
\& my $val = $foo{\*(Aqbar\*(Aq}{$key}; |
| 533 |
\& ... |
| 534 |
\& } |
| 535 |
.Ve |
| 536 |
.PP |
| 537 |
This module supports dual-valued variables created using \f(CWdualvar()\fR from |
| 538 |
Scalar::Util. However, while \f(CW$!\fR acts |
| 539 |
like a dualvar, it is implemented as a tied SV. To propagate its value, use |
| 540 |
the follow construct, if needed: |
| 541 |
.PP |
| 542 |
.Vb 1 |
| 543 |
\& my $errno :shared = dualvar($!,$!); |
| 544 |
.Ve |
| 545 |
.PP |
| 546 |
View existing bug reports at, and submit any new bugs, problems, patches, etc. |
| 547 |
to: <http://rt.cpan.org/Public/Dist/Display.html?Name=threads\-shared> |
| 548 |
.SH "SEE ALSO" |
| 549 |
.IX Header "SEE ALSO" |
| 550 |
threads::shared on MetaCPAN: |
| 551 |
<https://metacpan.org/release/threads\-shared> |
| 552 |
.PP |
| 553 |
Code repository for CPAN distribution: |
| 554 |
<https://github.com/Dual\-Life/threads\-shared> |
| 555 |
.PP |
| 556 |
threads, perlthrtut |
| 557 |
.PP |
| 558 |
<http://www.perl.com/pub/a/2002/06/11/threads.html> and |
| 559 |
<http://www.perl.com/pub/a/2002/09/04/threads.html> |
| 560 |
.PP |
| 561 |
Perl threads mailing list: |
| 562 |
<http://lists.perl.org/list/ithreads.html> |
| 563 |
.PP |
| 564 |
Sample code in the \fIexamples\fR directory of this distribution on CPAN. |
| 565 |
.SH AUTHOR |
| 566 |
.IX Header "AUTHOR" |
| 567 |
Artur Bergman <sky AT crucially DOT net> |
| 568 |
.PP |
| 569 |
Documentation borrowed from the old Thread.pm. |
| 570 |
.PP |
| 571 |
CPAN version produced by Jerry D. Hedden <jdhedden AT cpan DOT org>. |
| 572 |
.SH LICENSE |
| 573 |
.IX Header "LICENSE" |
| 574 |
threads::shared is released under the same license as Perl. |