1 package threads::shared;
9 unless ($Config{useithreads}) {
10 my @caller = caller(2);
12 $caller[1] line $caller[2]:
14 This Perl hasn't been configured and built properly for the threads
15 module to work. (The 'useithreads' configuration option hasn't been used.)
17 Having threads support requires all of Perl and all of the modules in
18 the Perl installation to be rebuilt, it is not just a question of adding
19 the threads module. (In other words, threaded and non-threaded Perls
20 are binary incompatible.)
22 If you want to the use the threads module, please contact the people
25 Cannot continue, aborting.
31 our @ISA = qw(Exporter);
32 our @EXPORT = qw(share cond_wait cond_broadcast cond_signal _refcnt _id _thrcnt);
33 our $VERSION = '0.90';
35 if ($Config{'useithreads'}) {
36 *cond_wait = \&cond_wait_enabled;
37 *cond_signal = \&cond_signal_enabled;
38 *cond_broadcast = \&cond_broadcast_enabled;
40 XSLoader::load('threads::shared',$VERSION);
43 *share = \&share_disabled;
44 *cond_wait = \&cond_wait_disabled;
45 *cond_signal = \&cond_signal_disabled;
46 *cond_broadcast = \&cond_broadcast_disabled;
50 sub cond_wait_disabled { return @_ };
51 sub cond_signal_disabled { return @_};
52 sub cond_broadcast_disabled { return @_};
53 sub share_disabled { return @_}
55 $threads::shared::threads_shared = 1;
59 sub threads::shared::tie::SPLICE
61 die "Splice not implemented for shared arrays";
68 threads::shared - Perl extension for sharing data structures between threads
77 my($scalar, @array, %hash);
82 $hash{bar} = share({});
87 cond_broadcast(@array);
92 By default, variables are private to each thread, and each newly created
93 thread gets a private copy of each existing variable. This module allows
94 you to share variables across different threads (and pseudoforks on
95 win32). It is used together with the threads module.
99 C<share>, C<lock>, C<cond_wait>, C<cond_signal>, C<cond_broadcast>
107 C<share> takes a value and marks it as shared. You can share a scalar, array,
108 hash, scalar ref, array ref or hash ref. C<share> will return the shared value.
110 C<share> will traverse up references exactly I<one> level.
111 C<share(\$a)> is equivalent to C<share($a)>, while C<share(\\$a)> is not.
113 A variable can also be marked as shared at compile time by using the
114 C<shared> attribute: C<my $var : shared>.
118 C<lock> places a lock on a variable until the lock goes out of scope. If
119 the variable is locked by another thread, the C<lock> call will block until
120 it's available. C<lock> is recursive, so multiple calls to C<lock> are
121 safe -- the variable will remain locked until the outermost lock on the
122 variable goes out of scope.
124 If a container object, such as a hash or array, is locked, all the elements
125 of that container are not locked. For example, if a thread does a C<lock
126 @a>, any other thread doing a C<lock($a[12])> won't block.
128 C<lock> will traverse up references exactly I<one> level.
129 C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
131 Note that you cannot explicitly unlock a variable; you can only wait for
132 the lock to go out of scope. If you need more fine-grained control, see
133 L<threads::shared::semaphore>.
135 =item cond_wait VARIABLE
137 The C<cond_wait> function takes a B<locked> variable as a parameter,
138 unlocks the variable, and blocks until another thread does a C<cond_signal>
139 or C<cond_broadcast> for that same locked variable. The variable that
140 C<cond_wait> blocked on is relocked after the C<cond_wait> is satisfied.
141 If there are multiple threads C<cond_wait>ing on the same variable, all but
142 one will reblock waiting to reacquire the lock on the variable. (So if
143 you're only using C<cond_wait> for synchronisation, give up the lock as
144 soon as possible). The two actions of unlocking the variable and entering
145 the blocked wait state are atomic, The two actions of exiting from the
146 blocked wait state and relocking the variable are not.
148 It is important to note that the variable can be notified even if no
149 thread C<cond_signal> or C<cond_broadcast> on the variable. It is therefore
150 important to check the value of the variable and go back to waiting if the
151 requirement is not fulfilled.
153 =item cond_signal VARIABLE
155 The C<cond_signal> function takes a B<locked> variable as a parameter and
156 unblocks one thread that's C<cond_wait>ing on that variable. If more than
157 one thread is blocked in a C<cond_wait> on that variable, only one (and
158 which one is indeterminate) will be unblocked.
160 If there are no threads blocked in a C<cond_wait> on the variable, the
161 signal is discarded. By always locking before signaling, you can (with
162 care), avoid signaling before another thread has entered cond_wait().
164 C<cond_signal> will normally generate a warning if you attempt to use it
165 on an unlocked variable. On the rare occasions where doing this may be
166 sensible, you can skip the warning with
168 { no warnings 'threads'; cond_signal($foo) }
170 =item cond_broadcast VARIABLE
172 The C<cond_broadcast> function works similarly to C<cond_signal>.
173 C<cond_broadcast>, though, will unblock B<all> the threads that are blocked
174 in a C<cond_wait> on the locked variable, rather than only one.
180 threads::shared is designed to disable itself silently if threads are
181 not available. If you want access to threads, you must C<use threads>
182 before you C<use threads::shared>. threads will emit a warning if you
183 use it after threads::shared.
187 C<bless> is not supported on shared references. In the current version,
188 C<bless> will only bless the thread local reference and the blessing
189 will not propagate to the other threads. This is expected to be
190 implemented in a future version of Perl.
192 Does not support splice on arrays!
196 Arthur Bergman E<lt>arthur at contiller.seE<gt>
198 threads::shared is released under the same license as Perl
200 Documentation borrowed from Thread.pm
204 L<threads>, L<perlthrtut>