Install Perl 5.8.8
[msysgit.git] / mingw / html / lib / Thread.html
blobab4294224ebebd786328d7c3c4d50ea2909e99e8
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>Thread - manipulate threads in Perl</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;Thread - manipulate threads in Perl</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#caveat">CAVEAT</a></li>
24 <li><a href="#synopsis">SYNOPSIS</a></li>
25 <li><a href="#description">DESCRIPTION</a></li>
26 <li><a href="#functions">FUNCTIONS</a></li>
27 <li><a href="#methods">METHODS</a></li>
28 <li><a href="#limitations">LIMITATIONS</a></li>
29 <li><a href="#see_also">SEE ALSO</a></li>
30 </ul>
31 <!-- INDEX END -->
33 <hr />
34 <p>
35 </p>
36 <h1><a name="name">NAME</a></h1>
37 <p>Thread - manipulate threads in Perl (for old code only)</p>
38 <p>
39 </p>
40 <hr />
41 <h1><a name="caveat">CAVEAT</a></h1>
42 <p>Perl has two thread models.</p>
43 <p>In Perl 5.005 the thread model was that all data is implicitly shared
44 and shared access to data has to be explicitly synchronized.
45 This model is called ``5005threads''.</p>
46 <p>In Perl 5.6 a new model was introduced in which all is was thread
47 local and shared access to data has to be explicitly declared.
48 This model is called ``ithreads'', for ``interpreter threads''.</p>
49 <p>In Perl 5.6 the ithreads model was not available as a public API,
50 only as an internal API that was available for extension writers,
51 and to implement <code>fork()</code> emulation on Win32 platforms.</p>
52 <p>In Perl 5.8 the ithreads model became available through the <code>threads</code>
53 module.</p>
54 <p>Neither model is configured by default into Perl (except, as mentioned
55 above, in Win32 ithreads are always available.) You can see your
56 Perl's threading configuration by running <code>perl -V</code> and looking for
57 the <em>use...threads</em> variables, or inside script by <code>use Config;</code>
58 and testing for <code>$Config{use5005threads}</code> and <code>$Config{useithreads}</code>.</p>
59 <p>For old code and interim backwards compatibility, the Thread module
60 has been reworked to function as a frontend for both 5005threads and
61 ithreads.</p>
62 <p>Note that the compatibility is not complete: because the data sharing
63 models are directly opposed, anything to do with data sharing has to
64 be thought differently. With the ithreads you must explicitly <code>share()</code>
65 variables between the threads.</p>
66 <p>For new code the use of the <code>Thread</code> module is discouraged and
67 the direct use of the <code>threads</code> and <code>threads::shared</code> modules
68 is encouraged instead.</p>
69 <p>Finally, note that there are many known serious problems with the
70 5005threads, one of the least of which is that regular expression
71 match variables like $1 are not threadsafe, that is, they easily get
72 corrupted by competing threads. Other problems include more insidious
73 data corruption and mysterious crashes. You are seriously urged to
74 use ithreads instead.</p>
75 <p>
76 </p>
77 <hr />
78 <h1><a name="synopsis">SYNOPSIS</a></h1>
79 <pre>
80 use Thread;</pre>
81 <pre>
82 my $t = Thread-&gt;new(\&amp;start_sub, @start_args);</pre>
83 <pre>
84 $result = $t-&gt;join;
85 $result = $t-&gt;eval;
86 $t-&gt;detach;</pre>
87 <pre>
88 if ($t-&gt;done) {
89 $t-&gt;join;
90 }</pre>
91 <pre>
92 if($t-&gt;equal($another_thread)) {
93 # ...
94 }</pre>
95 <pre>
96 yield();</pre>
97 <pre>
98 my $tid = Thread-&gt;self-&gt;tid;</pre>
99 <pre>
100 lock($scalar);
101 lock(@array);
102 lock(%hash);</pre>
103 <pre>
104 lock(\&amp;sub); # not available with ithreads</pre>
105 <pre>
106 $flags = $t-&gt;flags; # not available with ithreads</pre>
107 <pre>
108 my @list = Thread-&gt;list; # not available with ithreads</pre>
109 <pre>
110 use Thread 'async';</pre>
112 </p>
113 <hr />
114 <h1><a name="description">DESCRIPTION</a></h1>
115 <p>The <code>Thread</code> module provides multithreading support for perl.</p>
117 </p>
118 <hr />
119 <h1><a name="functions">FUNCTIONS</a></h1>
120 <dl>
121 <dt><strong><a name="item_new">$thread = Thread-&gt;<code>new(\&amp;start_sub)</code></a></strong>
123 <dt><strong>$thread = Thread-&gt;new(\&amp;start_sub, LIST)</strong>
125 <dd>
126 <p><a href="#item_new"><code>new</code></a> starts a new thread of execution in the referenced subroutine. The
127 optional list is passed as parameters to the subroutine. Execution
128 continues in both the subroutine and the code after the <a href="#item_new"><code>new</code></a> call.</p>
129 </dd>
130 <dd>
131 <p><code>Thread-&amp;gt;new</code> returns a thread object representing the newly created
132 thread.</p>
133 </dd>
134 </li>
135 <dt><strong><a name="item_lock">lock VARIABLE</a></strong>
137 <dd>
138 <p><a href="#item_lock"><code>lock</code></a> places a lock on a variable until the lock goes out of scope.</p>
139 </dd>
140 <dd>
141 <p>If the variable is locked by another thread, the <a href="#item_lock"><code>lock</code></a> call will
142 block until it's available. <a href="#item_lock"><code>lock</code></a> is recursive, so multiple calls
143 to <a href="#item_lock"><code>lock</code></a> are safe--the variable will remain locked until the
144 outermost lock on the variable goes out of scope.</p>
145 </dd>
146 <dd>
147 <p>Locks on variables only affect <a href="#item_lock"><code>lock</code></a> calls--they do <em>not</em> affect normal
148 access to a variable. (Locks on subs are different, and covered in a bit.)
149 If you really, <em>really</em> want locks to block access, then go ahead and tie
150 them to something and manage this yourself. This is done on purpose.
151 While managing access to variables is a good thing, Perl doesn't force
152 you out of its living room...</p>
153 </dd>
154 <dd>
155 <p>If a container object, such as a hash or array, is locked, all the
156 elements of that container are not locked. For example, if a thread
157 does a <code>lock @a</code>, any other thread doing a <a href="#item_lock"><code>lock($a[12])</code></a> won't
158 block.</p>
159 </dd>
160 <dd>
161 <p>With 5005threads you may also <a href="#item_lock"><code>lock</code></a> a sub, using <code>lock &amp;sub</code>.
162 Any calls to that sub from another thread will block until the lock
163 is released. This behaviour is not equivalent to declaring the sub
164 with the <code>locked</code> attribute. The <code>locked</code> attribute serializes
165 access to a subroutine, but allows different threads non-simultaneous
166 access. <code>lock &amp;sub</code>, on the other hand, will not allow <em>any</em> other
167 thread access for the duration of the lock.</p>
168 </dd>
169 <dd>
170 <p>Finally, <a href="#item_lock"><code>lock</code></a> will traverse up references exactly <em>one</em> level.
171 <a href="#item_lock"><code>lock(\$a)</code></a> is equivalent to <a href="#item_lock"><code>lock($a)</code></a>, while <a href="#item_lock"><code>lock(\\$a)</code></a> is not.</p>
172 </dd>
173 </li>
174 <dt><strong><a name="item_async_block_3b">async BLOCK;</a></strong>
176 <dd>
177 <p><code>async</code> creates a thread to execute the block immediately following
178 it. This block is treated as an anonymous sub, and so must have a
179 semi-colon after the closing brace. Like <code>Thread-&amp;gt;new</code>, <code>async</code>
180 returns a thread object.</p>
181 </dd>
182 </li>
183 <dt><strong><a name="item_self">Thread-&gt;self</a></strong>
185 <dd>
186 <p>The <a href="#item_self"><code>Thread-&gt;self</code></a> function returns a thread object that represents
187 the thread making the <a href="#item_self"><code>Thread-&gt;self</code></a> call.</p>
188 </dd>
189 </li>
190 <dt><strong><a name="item_cond_wait">cond_wait VARIABLE</a></strong>
192 <dd>
193 <p>The <a href="#item_cond_wait"><code>cond_wait</code></a> function takes a <strong>locked</strong> variable as
194 a parameter, unlocks the variable, and blocks until another thread
195 does a <a href="#item_cond_signal"><code>cond_signal</code></a> or <a href="#item_cond_broadcast"><code>cond_broadcast</code></a> for that same locked
196 variable. The variable that <a href="#item_cond_wait"><code>cond_wait</code></a> blocked on is relocked
197 after the <a href="#item_cond_wait"><code>cond_wait</code></a> is satisfied. If there are multiple threads
198 <a href="#item_cond_wait"><code>cond_wait</code></a>ing on the same variable, all but one will reblock waiting
199 to reaquire the lock on the variable. (So if you're only using
200 <a href="#item_cond_wait"><code>cond_wait</code></a> for synchronization, give up the lock as soon as
201 possible.)</p>
202 </dd>
203 </li>
204 <dt><strong><a name="item_cond_signal">cond_signal VARIABLE</a></strong>
206 <dd>
207 <p>The <a href="#item_cond_signal"><code>cond_signal</code></a> function takes a locked variable as a parameter and
208 unblocks one thread that's <a href="#item_cond_wait"><code>cond_wait</code></a>ing on that variable. If more than
209 one thread is blocked in a <a href="#item_cond_wait"><code>cond_wait</code></a> on that variable, only one (and
210 which one is indeterminate) will be unblocked.</p>
211 </dd>
212 <dd>
213 <p>If there are no threads blocked in a <a href="#item_cond_wait"><code>cond_wait</code></a> on the variable,
214 the signal is discarded.</p>
215 </dd>
216 </li>
217 <dt><strong><a name="item_cond_broadcast">cond_broadcast VARIABLE</a></strong>
219 <dd>
220 <p>The <a href="#item_cond_broadcast"><code>cond_broadcast</code></a> function works similarly to <a href="#item_cond_signal"><code>cond_signal</code></a>.
221 <a href="#item_cond_broadcast"><code>cond_broadcast</code></a>, though, will unblock <strong>all</strong> the threads that are
222 blocked in a <a href="#item_cond_wait"><code>cond_wait</code></a> on the locked variable, rather than only
223 one.</p>
224 </dd>
225 </li>
226 <dt><strong><a name="item_yield">yield</a></strong>
228 <dd>
229 <p>The <a href="#item_yield"><code>yield</code></a> function allows another thread to take control of the
230 CPU. The exact results are implementation-dependent.</p>
231 </dd>
232 </li>
233 </dl>
235 </p>
236 <hr />
237 <h1><a name="methods">METHODS</a></h1>
238 <dl>
239 <dt><strong><a name="item_join">join</a></strong>
241 <dd>
242 <p><a href="#item_join"><code>join</code></a> waits for a thread to end and returns any values the thread
243 exited with. <a href="#item_join"><code>join</code></a> will block until the thread has ended, though
244 it won't block if the thread has already terminated.</p>
245 </dd>
246 <dd>
247 <p>If the thread being <a href="#item_join"><code>join</code></a>ed <code>die</code>d, the error it died with will
248 be returned at this time. If you don't want the thread performing
249 the <a href="#item_join"><code>join</code></a> to die as well, you should either wrap the <a href="#item_join"><code>join</code></a> in
250 an <a href="#item_eval"><code>eval</code></a> or use the <a href="#item_eval"><code>eval</code></a> thread method instead of <a href="#item_join"><code>join</code></a>.</p>
251 </dd>
252 </li>
253 <dt><strong><a name="item_eval">eval</a></strong>
255 <dd>
256 <p>The <a href="#item_eval"><code>eval</code></a> method wraps an <a href="#item_eval"><code>eval</code></a> around a <a href="#item_join"><code>join</code></a>, and so waits for
257 a thread to exit, passing along any values the thread might have returned.
258 Errors, of course, get placed into <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$@</code></a>. (Not available with ithreads.)</p>
259 </dd>
260 </li>
261 <dt><strong><a name="item_detach">detach</a></strong>
263 <dd>
264 <p><a href="#item_detach"><code>detach</code></a> tells a thread that it is never going to be joined i.e.
265 that all traces of its existence can be removed once it stops running.
266 Errors in detached threads will not be visible anywhere - if you want
267 to catch them, you should use $SIG{__DIE__} or something like that.</p>
268 </dd>
269 </li>
270 <dt><strong><a name="item_equal">equal</a></strong>
272 <dd>
273 <p><a href="#item_equal"><code>equal</code></a> tests whether two thread objects represent the same thread and
274 returns true if they do.</p>
275 </dd>
276 </li>
277 <dt><strong><a name="item_tid">tid</a></strong>
279 <dd>
280 <p>The <a href="#item_tid"><code>tid</code></a> method returns the tid of a thread. The tid is
281 a monotonically increasing integer assigned when a thread is
282 created. The main thread of a program will have a tid of zero,
283 while subsequent threads will have tids assigned starting with one.</p>
284 </dd>
285 </li>
286 <dt><strong><a name="item_flags">flags</a></strong>
288 <dd>
289 <p>The <a href="#item_flags"><code>flags</code></a> method returns the flags for the thread. This is the
290 integer value corresponding to the internal flags for the thread,
291 and the value may not be all that meaningful to you.
292 (Not available with ithreads.)</p>
293 </dd>
294 </li>
295 <dt><strong><a name="item_done">done</a></strong>
297 <dd>
298 <p>The <a href="#item_done"><code>done</code></a> method returns true if the thread you're checking has
299 finished, and false otherwise. (Not available with ithreads.)</p>
300 </dd>
301 </li>
302 </dl>
304 </p>
305 <hr />
306 <h1><a name="limitations">LIMITATIONS</a></h1>
307 <p>The sequence number used to assign tids is a simple integer, and no
308 checking is done to make sure the tid isn't currently in use. If a
309 program creates more than 2**32 - 1 threads in a single run, threads
310 may be assigned duplicate tids. This limitation may be lifted in
311 a future version of Perl.</p>
313 </p>
314 <hr />
315 <h1><a name="see_also">SEE ALSO</a></h1>
316 <p><a href="file://C|\msysgit\mingw\html/lib/threads/shared.html">the threads::shared manpage</a> (not available with 5005threads)</p>
317 <p><a href="file://C|\msysgit\mingw\html/lib/attributes.html">the attributes manpage</a>, <a href="file://C|\msysgit\mingw\html/lib/Thread/Queue.html">the Thread::Queue manpage</a>, <a href="file://C|\msysgit\mingw\html/lib/Thread/Semaphore.html">the Thread::Semaphore manpage</a>,
318 <a href="file://C|\msysgit\mingw\html/lib/Thread/Specific.html">the Thread::Specific manpage</a> (not available with ithreads)</p>
319 <table border="0" width="100%" cellspacing="0" cellpadding="3">
320 <tr><td class="block" style="background-color: #cccccc" valign="middle">
321 <big><strong><span class="block">&nbsp;Thread - manipulate threads in Perl</span></strong></big>
322 </td></tr>
323 </table>
325 </body>
327 </html>