threads-0.99 >
1.72
との差分
threads 1.72 と 0.99 の差分
1 | 1 | |
2 | 2 | =encoding euc-jp |
3 | 3 | |
4 | =head1 | |
4 | =head1 名前 | |
5 | 5 | |
6 | ||
6 | threads - インタプリタスレッドの使用を可能にするPerl拡張 | |
7 | 7 | |
8 | ||
8 | =head1 概要 | |
9 | 9 | |
10 | ||
10 | use threads; | |
11 | 11 | |
12 | threads - Perl のインタプリタベースのスレッド | |
13 | ||
14 | =head1 VERSION | |
15 | ||
16 | =begin original | |
17 | ||
18 | This document describes threads version 1.72 | |
19 | ||
20 | =end original | |
21 | ||
22 | この文書は threads バージョン 1.72 を記述しています。 | |
23 | ||
24 | =head1 SYNOPSIS | |
25 | ||
26 | use threads ('yield', | |
27 | 'stack_size' => 64*4096, | |
28 | 'exit' => 'threads_only', | |
29 | 'stringify'); | |
30 | ||
31 | 12 | sub start_thread { |
32 | | |
13 | print "Thread started\n"; | |
33 | print('Thread started: ', join(' ', @args), "\n"); | |
34 | 14 | } |
35 | my $thr = threads->create('start_thread', 'argument'); | |
36 | $thr->join(); | |
37 | 15 | |
38 | threads->create(s | |
16 | my $thread = threads->create("start_thread","argument"); | |
17 | my $thread2 = $thread->create(sub { print "I am a thread"},"argument"); | |
18 | my $thread3 = async { foreach (@files) { ... } }; | |
39 | 19 | |
40 | | |
20 | $thread->join(); | |
41 | $thr | |
21 | $thread->detach(); | |
42 | if (my $err = $thr2->error()) { | |
43 | warn("Thread error: $err\n"); | |
44 | } | |
45 | 22 | |
46 | | |
23 | $thread = threads->self(); | |
47 | | |
24 | $thread = threads->object( $tid ); | |
48 | # or specify list context explicitly | |
49 | my $thr = threads->create({'context' => 'list'}, | |
50 | sub { return (qw/a b c/); }); | |
51 | my @results = $thr->join(); | |
52 | 25 | |
53 | $thr-> | |
26 | $thread->tid(); | |
27 | threads->tid(); | |
28 | threads->self->tid(); | |
54 | 29 | |
55 | # Get a thread's object | |
56 | $thr = threads->self(); | |
57 | $thr = threads->object($tid); | |
58 | ||
59 | # Get a thread's ID | |
60 | $tid = threads->tid(); | |
61 | $tid = $thr->tid(); | |
62 | $tid = "$thr"; | |
63 | ||
64 | # Give other threads a chance to run | |
65 | 30 | threads->yield(); |
66 | yield(); | |
67 | 31 | |
68 | | |
32 | threads->list(); | |
69 | my @threads = threads->list(); | |
70 | my $thread_count = threads->list(); | |
71 | 33 | |
72 | ||
34 | =head1 説明 | |
73 | my @joinable = threads->list(threads::joinable); | |
74 | 35 | |
75 | | |
36 | Perl 5.6 introduced something called interpreter threads. Interpreter | |
76 | | |
37 | threads are different from "5005threads" (the thread model of Perl | |
77 | | |
38 | 5.005) by creating a new perl interpreter per thread and not sharing | |
78 | | |
39 | any data or state between threads by default. | |
79 | 40 | |
80 | ||
41 | Perl 5.6 はインタープリッタ・スレッドと呼ばれるものを導入した。 | |
81 | ||
42 | インタープリッタ・スレッドは、スレッド毎に新たにPerlインタプリタを生成することによって、 | |
82 | ||
43 | また、デフォルトではいかなるデータや状態もスレッド間で共有しないことによって、 | |
44 | 5005スレッド(Perl 5.005 におけるスレッドモデル)とは区別される。 | |
83 | 45 | |
84 | # Create a thread with a specific context and stack size | |
85 | my $thr = threads->create({ 'context' => 'list', | |
86 | 'stack_size' => 32*4096, | |
87 | 'exit' => 'thread_only' }, | |
88 | \&foo); | |
89 | 46 | |
90 | | |
47 | Prior to perl 5.8 this has only been available to people embedding | |
91 | | |
48 | perl and for emulating fork() on windows. | |
92 | 49 | |
93 | ||
50 | perl 5.8より前では、これはperlを埋め込む(embedding)人々にとって、 | |
94 | ||
51 | そしてwindowsでfork()をエミュレートするためにのみ利用可能であった。 | |
95 | sleep(1); | |
96 | } | |
97 | if ($thr->is_joinable()) { | |
98 | $thr->join(); | |
99 | } | |
100 | 52 | |
101 | | |
53 | The threads API is loosely based on the old Thread.pm API. It is very | |
102 | | |
54 | important to note that variables are not shared between threads, all | |
55 | variables are per default thread local. To use shared variables one | |
56 | must use threads::shared. | |
103 | 57 | |
104 | ||
58 | threads APIは、いい加減な形で古いThread.pm APIに基づいている。 | |
105 | threads->exit(); | |
106 | ||
107 | =head1 DESCRIPTION | |
108 | ||
109 | =begin original | |
110 | ||
111 | Perl 5.6 introduced something called interpreter threads. Interpreter threads | |
112 | are different from I<5005threads> (the thread model of Perl 5.005) by creating | |
113 | a new Perl interpreter per thread, and not sharing any data or state between | |
114 | threads by default. | |
115 | ||
116 | =end original | |
117 | ||
118 | Perl 5.6 はインタプリタスレッドと呼ばれるものを導入しました。 | |
119 | インタプリタスレッドは、スレッド毎に新たに Perl インタプリタを | |
120 | 生成することによって、また、デフォルトではいかなるデータや状態も | |
121 | スレッド間で共有しないことによって、I<5005スレッド> | |
122 | (Perl 5.005 におけるスレッドモデル)とは区別されます。 | |
123 | ||
124 | =begin original | |
125 | ||
126 | Prior to Perl 5.8, this has only been available to people embedding Perl, and | |
127 | for emulating fork() on Windows. | |
128 | ||
129 | =end original | |
130 | ||
131 | Perl 5.8 より前では、これは Perl を組み込むする人々にとってのみ、 | |
132 | そして Windows で fork() をエミュレートするためにのみ利用可能でした。 | |
133 | ||
134 | =begin original | |
135 | ||
136 | The I<threads> API is loosely based on the old Thread.pm API. It is very | |
137 | important to note that variables are not shared between threads, all variables | |
138 | are by default thread local. To use shared variables one must also use | |
139 | L<threads::shared>: | |
140 | ||
141 | =end original | |
142 | ||
143 | I<threads> API は、古い Thread.pm API におおまかに基づいています。 | |
144 | 59 | 変数はスレッド間で共有されず、全ての変数はデフォルトで |
145 | スレッドローカルなものであることに注意しておくことが非常に重要 | |
60 | スレッドローカルなものであることに注意しておくことが非常に重要だ。 | |
146 | 共有変数を利用するには、 | |
61 | 共有変数を利用するには、threads::sharedを使わなければならない。 | |
147 | 62 | |
148 | use threads | |
63 | It is also important to note that you must enable threads by doing | |
149 | ||
64 | C<use threads> as early as possible in the script itself and that it | |
65 | is not possible to enable threading inside an C<eval "">, C<do>, | |
66 | C<require>, or C<use>. In particular, if you are intending to share | |
67 | variables with threads::shared, you must C<use threads> before you | |
68 | C<use threads::shared> and C<threads> will emit a warning if you do | |
69 | it the other way around. | |
150 | 70 | |
151 | ||
71 | また、スクリプト内ではできるだけ早いうちにC<use threads>して | |
152 | ||
153 | It is also important to note that you must enable threads by doing C<use | |
154 | threads> as early as possible in the script itself, and that it is not | |
155 | possible to enable threading inside an C<eval "">, C<do>, C<require>, or | |
156 | C<use>. In particular, if you are intending to share variables with | |
157 | L<threads::shared>, you must C<use threads> before you C<use threads::shared>. | |
158 | (C<threads> will emit a warning if you do it the other way around.) | |
159 | ||
160 | =end original | |
161 | ||
162 | また、スクリプト内ではできるだけ早いうちに C<use threads> して | |
163 | 72 | スレッドを利用可能にしておくべきだし、 |
164 | C<eval "">, C<do>, | |
73 | C<eval "">, C<do>,C<require>, or C<use>の内部では | |
165 | スレッド操作ができないことに注意 | |
74 | スレッド操作ができないことに注意すること。 | |
166 | 特に | |
75 | 特にthreads::sharedを使って変数を共有しようとするならば、 | |
167 | C<use threads::shared> | |
76 | C<use threads::shared>の前にC<use threads>しなければならない。 | |
168 | ||
77 | 逆にしてしまうとC<threads>は警告を発する。 | |
169 | 78 | |
170 | 79 | =over |
171 | 80 | |
172 | =item $thr = threads->create( | |
81 | =item $thread = threads->create(function, LIST) | |
173 | 82 | |
174 | ||
83 | This will create a new thread with the entry point function and give | |
84 | it LIST as parameters. It will return the corresponding threads | |
85 | object. The new() method is an alias for create(). | |
175 | 86 | |
176 | ||
87 | これはエントリーポイントとなる関数を伴って新しいスレッドを生成し、 | |
177 | ||
88 | リストをパラメータとして与える。対応するスレッドオブジェクトを返す。new()はcreate()の言い換えだ。 | |
178 | return the corresponding threads object, or C<undef> if thread creation failed. | |
179 | 89 | |
180 | =e | |
90 | =item $thread->join | |
181 | 91 | |
182 | ||
92 | This will wait for the corresponding thread to join. When the thread | |
183 | ||
93 | finishes, join() will return the return values of the entry point | |
184 | ||
94 | function. If the thread has been detached, an error will be thrown. | |
185 | ||
95 | If the program exits without all other threads having been either | |
96 | joined or detached, then a warning will be issued. (A program exits | |
97 | either because one of its threads explicitly calls exit(), or in the | |
98 | case of the main thread, reaches the end of the main program file.) | |
186 | 99 | |
187 | ||
100 | 対応するスレッドがjoinするのを待つ。そのスレッドが終了した時、join()はエントリーポイント関数の戻り値を返す。 | |
101 | もしそのスレッドがdetachされていたなら、エラーが投げられる。いずれかのスレッドがjoinかdetachされずにプログラムが終了すると、 | |
102 | 警告が出る(プログラムは、スレッドが明示的にexit()を呼び出すか、あるいはメインスレッドにおいてプログラムファイルの終端に達した時に終了する)。 | |
188 | 103 | |
189 | ||
104 | =item $thread->detach | |
190 | a code ref. | |
191 | 105 | |
192 | ||
106 | Will make the thread unjoinable, and cause any eventual return value | |
107 | to be discarded. | |
193 | 108 | |
194 | ||
109 | そのスレッドをjoin不可能にし、結果の戻り値を放棄する。 | |
195 | 110 | |
196 | ||
111 | =item threads->self | |
197 | # or | |
198 | my $thr = threads->create(sub { ... }, ...); | |
199 | # or | |
200 | my $thr = threads->create(\&func, ...); | |
201 | 112 | |
202 | ||
113 | This will return the thread object for the current thread. | |
203 | 114 | |
204 | ||
115 | 現在のスレッドのスレッドオブジェクトを返す。 | |
205 | 116 | |
206 | =e | |
117 | =item $thread->tid | |
207 | 118 | |
208 | ||
119 | This will return the id of the thread. Thread IDs are integers, with | |
120 | the main thread in a program being 0. Currently Perl assigns a unique | |
121 | tid to every thread ever created in your program, assigning the first | |
122 | thread to be created a tid of 1, and increasing the tid by 1 for each | |
123 | new thread that's created. | |
209 | 124 | |
210 | ||
125 | スレッドのidを返す。スレッドIDは整数であり、プログラムの始まりとなるメインスレッドの値は0である。 | |
126 | 現在のPerlは、生成された最初のスレッドのtidに1を与え、新しいスレッドが生成されるたびにtidの値を1増やしていくことによって、プログラム中に生成される全てのスレッドに一意なtidを割り振る。 | |
211 | 127 | |
212 | ||
128 | NB the class method C<< threads->tid() >> is a quick way to get the | |
129 | current thread id if you don't have your thread object handy. | |
213 | 130 | |
214 | ||
131 | 注意 あなたがスレッドオブジェクトを利用できない場合、クラスメソッドC<< threads->tid() >>は、現在のスレッドidを手にする近道である。 | |
215 | the thread finishes, C<-E<gt>join()> will return the return value(s) of the | |
216 | entry point function. | |
217 | 132 | |
218 | =e | |
133 | =item threads->object( tid ) | |
219 | 134 | |
220 | ||
135 | This will return the thread object for the thread associated with the | |
221 | ||
136 | specified tid. Returns undef if there is no thread associated with the tid | |
222 | ||
137 | or no tid is specified or the specified tid is undef. | |
223 | 138 | |
224 | ||
139 | 指定されたtidに関連しているスレッドのオブジェクトを返す。もしそのtidと関連しているスレッドがない場合、あるいはtidが指定されていない、指定されたtidがundefの場合、メソッドはundefを返す。 | |
225 | 140 | |
226 | ||
141 | =item threads->yield(); | |
227 | is determined at the time of thread creation. | |
228 | 142 | |
229 | =end original | |
230 | ||
231 | C<-E<gt>join()> のコンテキスト (無効、スカラ、リストのいずれか) は、 | |
232 | スレッド生成時に決定されます。 | |
233 | ||
234 | # Create thread in list context (implicit) | |
235 | my ($thr1) = threads->create(sub { | |
236 | my @results = qw(a b c); | |
237 | return (@results); | |
238 | }); | |
239 | # or (explicit) | |
240 | my $thr1 = threads->create({'context' => 'list'}, | |
241 | sub { | |
242 | my @results = qw(a b c); | |
243 | return (@results); | |
244 | }); | |
245 | # Retrieve list results from thread | |
246 | my @res1 = $thr1->join(); | |
247 | ||
248 | # Create thread in scalar context (implicit) | |
249 | my $thr2 = threads->create(sub { | |
250 | my $result = 42; | |
251 | return ($result); | |
252 | }); | |
253 | # Retrieve scalar result from thread | |
254 | my $res2 = $thr2->join(); | |
255 | ||
256 | # Create a thread in void context (explicit) | |
257 | my $thr3 = threads->create({'void' => 1}, | |
258 | sub { print("Hello, world\n"); }); | |
259 | # Join the thread in void context (i.e., no return value) | |
260 | $thr3->join(); | |
261 | ||
262 | =begin original | |
263 | ||
264 | See L</"THREAD CONTEXT"> for more details. | |
265 | ||
266 | =end original | |
267 | ||
268 | さらなる詳細については L</"THREAD CONTEXT"> を参照してください。 | |
269 | ||
270 | =begin original | |
271 | ||
272 | If the program exits without all threads having either been joined or | |
273 | detached, then a warning will be issued. | |
274 | ||
275 | =end original | |
276 | ||
277 | 全てのスレッドが join されるか detach される前にプログラムが終了した場合、 | |
278 | 警告が発生します。 | |
279 | ||
280 | =begin original | |
281 | ||
282 | Calling C<-E<gt>join()> or C<-E<gt>detach()> on an already joined thread will | |
283 | cause an error to be thrown. | |
284 | ||
285 | =end original | |
286 | ||
287 | 既に join しているスレッドに対して C<-E<gt>join()> や C<-E<gt>detach()> を | |
288 | 行うと、エラーが発生します。 | |
289 | ||
290 | =item $thr->detach() | |
291 | ||
292 | =begin original | |
293 | ||
294 | Makes the thread unjoinable, and causes any eventual return value to be | |
295 | discarded. When the program exits, any detached threads that are still | |
296 | running are silently terminated. | |
297 | ||
298 | =end original | |
299 | ||
300 | スレッドを join 不可能にし、最終的な返り値を捨てるようにします。 | |
301 | プログラムが終了するとき、まだ実行中の detach されたスレッドは暗黙に | |
302 | 終了します。 | |
303 | ||
304 | =begin original | |
305 | ||
306 | If the program exits without all threads having either been joined or | |
307 | detached, then a warning will be issued. | |
308 | ||
309 | =end original | |
310 | ||
311 | 全てのスレッドが join されるか detach される前にプログラムが終了した場合、 | |
312 | 警告が発生します。 | |
313 | ||
314 | =begin original | |
315 | ||
316 | Calling C<-E<gt>join()> or C<-E<gt>detach()> on an already detached thread | |
317 | will cause an error to be thrown. | |
318 | ||
319 | =end original | |
320 | ||
321 | 既に detach されたスレッドに C<-E<gt>join()> や C<-E<gt>detach()> を | |
322 | 呼び出すと、エラーが発生します。 | |
323 | ||
324 | =item threads->detach() | |
325 | ||
326 | =begin original | |
327 | ||
328 | Class method that allows a thread to detach itself. | |
329 | ||
330 | =end original | |
331 | ||
332 | スレッドが自分自身を detach するためのクラスメソッドです。 | |
333 | ||
334 | =item threads->self() | |
335 | ||
336 | =begin original | |
337 | ||
338 | Class method that allows a thread to obtain its own I<threads> object. | |
339 | ||
340 | =end original | |
341 | ||
342 | スレッドが自身の I<threads> オブジェクトを取得するためのクラスメソッドです。 | |
343 | ||
344 | =item $thr->tid() | |
345 | ||
346 | =begin original | |
347 | ||
348 | Returns the ID of the thread. Thread IDs are unique integers with the main | |
349 | thread in a program being 0, and incrementing by 1 for every thread created. | |
350 | ||
351 | =end original | |
352 | ||
353 | スレッドの ID を返します。 | |
354 | スレッド ID はユニークな整数であり、プログラムの始まりとなる | |
355 | メインスレッドの値は 0 で、 | |
356 | 新しいスレッドが生成されるたびに値を 1 増やしていきます。 | |
357 | ||
358 | =item threads->tid() | |
359 | ||
360 | =begin original | |
361 | ||
362 | Class method that allows a thread to obtain its own ID. | |
363 | ||
364 | =end original | |
365 | ||
366 | スレッドが自身の ID を得るためのクラスメソッドです。 | |
367 | ||
368 | =item "$thr" | |
369 | ||
370 | =begin original | |
371 | ||
372 | If you add the C<stringify> import option to your C<use threads> declaration, | |
373 | then using a threads object in a string or a string context (e.g., as a hash | |
374 | key) will cause its ID to be used as the value: | |
375 | ||
376 | =end original | |
377 | ||
378 | C<use threads> 宣言に C<stringify> インポートオプションを追加すると、 | |
379 | 文字列や文字列コンテキスト (例えばハッシュのキーとして) で | |
380 | スレッドオブジェクトを使おうとすると、その ID が値として使われます: | |
381 | ||
382 | use threads qw(stringify); | |
383 | ||
384 | my $thr = threads->create(...); | |
385 | print("Thread $thr started...\n"); # Prints out: Thread 1 started... | |
386 | ||
387 | =item threads->object($tid) | |
388 | ||
389 | =begin original | |
390 | ||
391 | This will return the I<threads> object for the I<active> thread associated | |
392 | with the specified thread ID. Returns C<undef> if there is no thread | |
393 | associated with the TID, if the thread is joined or detached, if no TID is | |
394 | specified or if the specified TID is undef. | |
395 | ||
396 | =end original | |
397 | ||
398 | 指定されたスレッドに関連するアクティブな I<threads> オブジェクトを返します。 | |
399 | もし TID で指定されたスレッドがない場合、join や detach されている場合、 | |
400 | TID が指定されていない場合、指定された TID が undef の | |
401 | 場合、メソッドは C<undef> を返します。 | |
402 | ||
403 | =item threads->yield() | |
404 | ||
405 | =begin original | |
406 | ||
407 | 143 | This is a suggestion to the OS to let this thread yield CPU time to other |
408 | 144 | threads. What actually happens is highly dependent upon the underlying |
409 | 145 | thread implementation. |
410 | 146 | |
411 | ||
147 | このスレッドが他のスレッドにCPU時間を譲ってもいいということをOSに示唆する。実際に起こることは、基になっているスレッド実装に大きく依存している。 | |
412 | 148 | |
413 | ||
149 | You may do C<use threads qw(yield)> then use just a bare C<yield> in your | |
414 | 示唆します。 | |
415 | 実際に起こることは、基になっているスレッド実装に大きく依存しています。 | |
416 | ||
417 | =begin original | |
418 | ||
419 | You may do C<use threads qw(yield)>, and then just use C<yield()> in your | |
420 | 150 | code. |
421 | 151 | |
422 | ||
152 | コード内では、C<use threads qw(yield)>してから、たんに裸のC<yield>を使ってもよい。 | |
423 | 153 | |
424 | ||
154 | =item threads->list(); | |
425 | 使えます。 | |
426 | 155 | |
427 | ||
156 | This will return a list of all non joined, non detached threads. | |
428 | 157 | |
429 | ||
158 | joinおよびdetachされていない全てのスレッドのリストを返す。 | |
430 | 159 | |
431 | =item threads->list(threads::running) | |
432 | ||
433 | =item threads->list(threads::joinable) | |
434 | ||
435 | =begin original | |
436 | ||
437 | With no arguments (or using C<threads::all>) and in a list context, returns a | |
438 | list of all non-joined, non-detached I<threads> objects. In a scalar context, | |
439 | returns a count of the same. | |
440 | ||
441 | =end original | |
442 | ||
443 | 引数なしで (または C<threads::all> を使って) リストコンテキストの場合、 | |
444 | join されておらず、detach されていない全ての I<threads> オブジェクトの | |
445 | リストを返します。 | |
446 | スカラコンテキストでは、上述のものの数を返します。 | |
447 | ||
448 | =begin original | |
449 | ||
450 | With a I<true> argument (using C<threads::running>), returns a list of all | |
451 | non-joined, non-detached I<threads> objects that are still running. | |
452 | ||
453 | =end original | |
454 | ||
455 | 引数が I<真> の (または C<threads::running> を使った) 場合、 | |
456 | join されておらず、detach されていない、まだ実行中の | |
457 | I<threads> オブジェクトのリストを返します。 | |
458 | ||
459 | =begin original | |
460 | ||
461 | With a I<false> argument (using C<threads::joinable>), returns a list of all | |
462 | non-joined, non-detached I<threads> objects that have finished running (i.e., | |
463 | for which C<-E<gt>join()> will not I<block>). | |
464 | ||
465 | =end original | |
466 | ||
467 | 引数が I<偽> の (または C<threads::joinable> を使った) 場合、 | |
468 | join されておらず、detach されていない、実行が終了した | |
469 | (つまり C<-E<gt>join()> が I<ブロック> されない) I<threads> オブジェクトの | |
470 | リストを返します。 | |
471 | ||
472 | =item $thr1->equal($thr2) | |
473 | ||
474 | =begin original | |
475 | ||
476 | Tests if two threads objects are the same thread or not. This is overloaded | |
477 | to the more natural forms: | |
478 | ||
479 | =end original | |
480 | ||
481 | 2 つのスレッドオブジェクトが同じスレッドかどうかをテストします。 | |
482 | これはより自然な形にオーバーロードされます: | |
483 | ||
484 | if ($thr1 == $thr2) { | |
485 | print("Threads are the same\n"); | |
486 | } | |
487 | # or | |
488 | if ($thr1 != $thr2) { | |
489 | print("Threads differ\n"); | |
490 | } | |
491 | ||
492 | =begin original | |
493 | ||
494 | (Thread comparison is based on thread IDs.) | |
495 | ||
496 | =end original | |
497 | ||
498 | (スレッドの比較はスレッド ID を基にします。) | |
499 | ||
500 | 160 | =item async BLOCK; |
501 | 161 | |
502 | =begin original | |
503 | ||
504 | 162 | C<async> creates a thread to execute the block immediately following |
505 | it. This block is treated as an anonymous sub | |
163 | it. This block is treated as an anonymous sub, and so must have a | |
506 | semicolon after the closing brace. | |
164 | semi-colon after the closing brace. Like C<< threads->new >>, C<async> | |
507 | returns a | |
165 | returns a thread object. | |
508 | 166 | |
509 | ||
167 | C<async>はその直後に続くブロックを実行するスレッドを生成する。このブロックは無名サブルーチンとして扱われるので、閉じブレースの後にセミコロンをつけなければならない。C<< threads->new >>同様、C<async>はスレッドオブジェクトを返す。 | |
510 | 168 | |
511 | C<async> はその直後に続くブロックを実行するスレッドを生成します。 | |
512 | このブロックは無名サブルーチンとして扱われるので、閉じ大括弧の後に | |
513 | セミコロンをつけなければなりません。 | |
514 | C<threads-E<gt>create()> 同様、C<async> は | |
515 | I<threads> オブジェクトを返します。 | |
516 | ||
517 | =item $thr->error() | |
518 | ||
519 | =begin original | |
520 | ||
521 | Threads are executed in an C<eval> context. This method will return C<undef> | |
522 | if the thread terminates I<normally>. Otherwise, it returns the value of | |
523 | C<$@> associated with the thread's execution status in its C<eval> context. | |
524 | ||
525 | =end original | |
526 | ||
527 | スレッドは C<無効> コンテキストで実行されます。 | |
528 | このメソッドは、スレッドが I<普通に> 終了した場合は C<undef> を返します。 | |
529 | さもなければ、スレッドの実行状態に関連づけられた C<$@> の値を | |
530 | C<無効> コンテキストで返します。 | |
531 | ||
532 | =item $thr->_handle() | |
533 | ||
534 | =begin original | |
535 | ||
536 | This I<private> method returns the memory location of the internal thread | |
537 | structure associated with a threads object. For Win32, this is a pointer to | |
538 | the C<HANDLE> value returned by C<CreateThread> (i.e., C<HANDLE *>); for other | |
539 | platforms, it is a pointer to the C<pthread_t> structure used in the | |
540 | C<pthread_create> call (i.e., C<pthread_t *>). | |
541 | ||
542 | =end original | |
543 | ||
544 | この I<プライベート> メソッドは、スレッドオブジェクトに関連づけられた | |
545 | 内部スレッド構造体のメモリ位置を返します。 | |
546 | Win32 では、これは C<CreateThread> から返される C<HANDLE> 値へのポインタ | |
547 | (つまり C<HANDLE *>) です; その他のプラットフォームでは、 | |
548 | C<pthread_create> 呼び出しで使われる C<pthread_t> 構造体へのポインタ | |
549 | (つまり C<pthread_t *>) です。 | |
550 | ||
551 | =begin original | |
552 | ||
553 | This method is of no use for general Perl threads programming. Its intent is | |
554 | to provide other (XS-based) thread modules with the capability to access, and | |
555 | possibly manipulate, the underlying thread structure associated with a Perl | |
556 | thread. | |
557 | ||
558 | =end original | |
559 | ||
560 | このメソッドは、一般的な Perl スレッドプログラミングには無用です。 | |
561 | このメソッドの目的は、その他の (XS ベースの) スレッドモジュールが、 | |
562 | Perl スレッドと関連づけられている基礎となるスレッド構造体へのアクセスおよび | |
563 | おそらくは操作を可能にすることです。 | |
564 | ||
565 | =item threads->_handle() | |
566 | ||
567 | =begin original | |
568 | ||
569 | Class method that allows a thread to obtain its own I<handle>. | |
570 | ||
571 | =end original | |
572 | ||
573 | スレッドが自身の I<handle> を得るためのクラスメソッドです。 | |
574 | ||
575 | 169 | =back |
576 | 170 | |
577 | =head1 | |
171 | =head1 警告 | |
578 | 172 | |
579 | (スレッドの終了) | |
580 | ||
581 | =begin original | |
582 | ||
583 | The usual method for terminating a thread is to | |
584 | L<return()|perlfunc/"return EXPR"> from the entry point function with the | |
585 | appropriate return value(s). | |
586 | ||
587 | =end original | |
588 | ||
589 | スレッドを終了するための通常の手法は、エントリポイント関数で | |
590 | 適切な返り値と共に L<return()|perlfunc/"return EXPR"> を使うことです。 | |
591 | ||
592 | =over | |
593 | ||
594 | =item threads->exit() | |
595 | ||
596 | =begin original | |
597 | ||
598 | If needed, a thread can be exited at any time by calling | |
599 | C<threads-E<gt>exit()>. This will cause the thread to return C<undef> in a | |
600 | scalar context, or the empty list in a list context. | |
601 | ||
602 | =end original | |
603 | ||
604 | もし必要なら、スレッドはいつでも C<threads-E<gt>exit()> を | |
605 | 呼び出すことで終了させることが出来ます。 | |
606 | これにより、スレッドはスカラコンテキストでは C<undef> を返し、 | |
607 | リストコンテキストでは空リストを返します。 | |
608 | ||
609 | =begin original | |
610 | ||
611 | When called from the I<main> thread, this behaves the same as C<exit(0)>. | |
612 | ||
613 | =end original | |
614 | ||
615 | I<main> スレッドから呼び出されると、C<exit(0)> と同様に振る舞います。 | |
616 | ||
617 | =item threads->exit(status) | |
618 | ||
619 | =begin original | |
620 | ||
621 | When called from a thread, this behaves like C<threads-E<gt>exit()> (i.e., the | |
622 | exit status code is ignored). | |
623 | ||
624 | =end original | |
625 | ||
626 | スレッドから呼び出されると、C<threads-E<gt>exit()> と同様に振る舞います | |
627 | (つまり、status 終了コードは無視されます)。 | |
628 | ||
629 | =begin original | |
630 | ||
631 | When called from the I<main> thread, this behaves the same as C<exit(status)>. | |
632 | ||
633 | =end original | |
634 | ||
635 | I<main> スレッドから呼び出されると、C<exit(status)> と同様に振る舞います。 | |
636 | ||
637 | =item die() | |
638 | ||
639 | =begin original | |
640 | ||
641 | Calling C<die()> in a thread indicates an abnormal exit for the thread. Any | |
642 | C<$SIG{__DIE__}> handler in the thread will be called first, and then the | |
643 | thread will exit with a warning message that will contain any arguments passed | |
644 | in the C<die()> call. | |
645 | ||
646 | =end original | |
647 | ||
648 | スレッドでの C<die()> の呼び出しは、スレッドの異常終了を意味します。 | |
649 | まずスレッドでの C<$SIG{__DIE__}> ハンドラが呼び出され、 | |
650 | それからスレッドは C<die()> 呼び出しに渡された引数による警告メッセージと | |
651 | 共に終了します。 | |
652 | ||
653 | =item exit(status) | |
654 | ||
655 | =begin original | |
656 | ||
657 | Calling L<exit()|perlfunc/"exit EXPR"> inside a thread causes the whole | |
658 | application to terminate. Because of this, the use of C<exit()> inside | |
659 | threaded code, or in modules that might be used in threaded applications, is | |
660 | strongly discouraged. | |
661 | ||
662 | =end original | |
663 | ||
664 | スレッドの内部で L<exit()|perlfunc/"exit EXPR"> を呼び出すと、 | |
665 | アプリケーション全体が終了します。 | |
666 | これのことにより、スレッドコード内部や、スレッド化された | |
667 | アプリケーションで使われるかも知れないモジュールでの C<exit()> の使用は | |
668 | 強く非推奨です。 | |
669 | ||
670 | =begin original | |
671 | ||
672 | If C<exit()> really is needed, then consider using the following: | |
673 | ||
674 | =end original | |
675 | ||
676 | もし本当に C<exit()> が必要なら、以下を使うことを考えてください: | |
677 | ||
678 | threads->exit() if threads->can('exit'); # Thread friendly | |
679 | exit(status); | |
680 | ||
681 | =item use threads 'exit' => 'threads_only' | |
682 | ||
683 | =begin original | |
684 | ||
685 | This globally overrides the default behavior of calling C<exit()> inside a | |
686 | thread, and effectively causes such calls to behave the same as | |
687 | C<threads-E<gt>exit()>. In other words, with this setting, calling C<exit()> | |
688 | causes only the thread to terminate. | |
689 | ||
690 | =end original | |
691 | ||
692 | これはスレッド内での C<exit()> 呼び出しのデフォルトの振る舞いをグローバルに | |
693 | 上書きし、事実上このような呼び出しを C<threads-E<gt>exit()> と同じ | |
694 | 振る舞いにします。 | |
695 | 言い換えると、この設定によって、C<exit()> を呼び出したときにスレッドだけを | |
696 | 終了させます。 | |
697 | ||
698 | =begin original | |
699 | ||
700 | Because of its global effect, this setting should not be used inside modules | |
701 | or the like. | |
702 | ||
703 | =end original | |
704 | ||
705 | これはグローバルな効果を持つので、この設定はモジュールのようなものの内部では | |
706 | 使うべきではありません。 | |
707 | ||
708 | =begin original | |
709 | ||
710 | The I<main> thread is unaffected by this setting. | |
711 | ||
712 | =end original | |
713 | ||
714 | I<main> スレッドはこの設定の影響を受けません。 | |
715 | ||
716 | =item threads->create({'exit' => 'thread_only'}, ...) | |
717 | ||
718 | =begin original | |
719 | ||
720 | This overrides the default behavior of C<exit()> inside the newly created | |
721 | thread only. | |
722 | ||
723 | =end original | |
724 | ||
725 | これは新しく作られたスレッドの内側でだけ C<exit()> のデフォルトの | |
726 | 振る舞いを上書きします。 | |
727 | ||
728 | =item $thr->set_thread_exit_only(boolean) | |
729 | ||
730 | =begin original | |
731 | ||
732 | This can be used to change the I<exit thread only> behavior for a thread after | |
733 | it has been created. With a I<true> argument, C<exit()> will cause only the | |
734 | thread to exit. With a I<false> argument, C<exit()> will terminate the | |
735 | application. | |
736 | ||
737 | =end original | |
738 | ||
739 | これは、スレッドの I<スレッドだけ終了> の振る舞いを、スレッドが作られた | |
740 | 後で変更するために使われます。 | |
741 | I<真> の値を渡すと、C<exit()> によってスレッドだけが終了します。 | |
742 | I<偽> の値を渡すと、C<exit()> によってアプリケーションが終了します。 | |
743 | ||
744 | =begin original | |
745 | ||
746 | The I<main> thread is unaffected by this call. | |
747 | ||
748 | =end original | |
749 | ||
750 | I<main> スレッドはこの呼び出しの影響を受けません。 | |
751 | ||
752 | =item threads->set_thread_exit_only(boolean) | |
753 | ||
754 | =begin original | |
755 | ||
756 | Class method for use inside a thread to change its own behavior for C<exit()>. | |
757 | ||
758 | =end original | |
759 | ||
760 | C<exit()> の振る舞いを変えるためにスレッドの内側で使うための | |
761 | クラスメソッドです。 | |
762 | ||
763 | =begin original | |
764 | ||
765 | The I<main> thread is unaffected by this call. | |
766 | ||
767 | =end original | |
768 | ||
769 | I<main> スレッドはこの呼び出しの影響を受けません。 | |
770 | ||
771 | =back | |
772 | ||
773 | =head1 THREAD STATE | |
774 | ||
775 | (スレッドの状態) | |
776 | ||
777 | =begin original | |
778 | ||
779 | The following boolean methods are useful in determining the I<state> of a | |
780 | thread. | |
781 | ||
782 | =end original | |
783 | ||
784 | 以下の真偽値メソッドはスレッドの I<状態> を決定するのに便利です。 | |
785 | ||
786 | =over | |
787 | ||
788 | =item $thr->is_running() | |
789 | ||
790 | =begin original | |
791 | ||
792 | Returns true if a thread is still running (i.e., if its entry point function | |
793 | has not yet finished or exited). | |
794 | ||
795 | =end original | |
796 | ||
797 | スレッドがまだ実行されている(つまり、そのエントリポイント関数がまだ完了または | |
798 | 終了していない)なら真を返します。 | |
799 | ||
800 | =item $thr->is_joinable() | |
801 | ||
802 | =begin original | |
803 | ||
804 | Returns true if the thread has finished running, is not detached and has not | |
805 | yet been joined. In other words, the thread is ready to be joined, and a call | |
806 | to C<$thr-E<gt>join()> will not I<block>. | |
807 | ||
808 | =end original | |
809 | ||
810 | スレッドが実行を完了していて、detach も join もされていないなら真を返します。 | |
811 | 言い換えると、このスレッドは join する準備が出来ていて、 | |
812 | C<$thr-E<gt>join()> の呼び出しは I<ブロック> されません。 | |
813 | ||
814 | =item $thr->is_detached() | |
815 | ||
816 | =begin original | |
817 | ||
818 | Returns true if the thread has been detached. | |
819 | ||
820 | =end original | |
821 | ||
822 | スレッドが detach されたなら真を返します。 | |
823 | ||
824 | =item threads->is_detached() | |
825 | ||
826 | =begin original | |
827 | ||
828 | Class method that allows a thread to determine whether or not it is detached. | |
829 | ||
830 | =end original | |
831 | ||
832 | スレッドが detach されているかどうかを決定できるようにするための | |
833 | クラスメソッドです。 | |
834 | ||
835 | =back | |
836 | ||
837 | =head1 THREAD CONTEXT | |
838 | ||
839 | (スレッドのコンテキスト) | |
840 | ||
841 | =begin original | |
842 | ||
843 | As with subroutines, the type of value returned from a thread's entry point | |
844 | function may be determined by the thread's I<context>: list, scalar or void. | |
845 | The thread's context is determined at thread creation. This is necessary so | |
846 | that the context is available to the entry point function via | |
847 | L<wantarray()|perlfunc/"wantarray">. The thread may then specify a value of | |
848 | the appropriate type to be returned from C<-E<gt>join()>. | |
849 | ||
850 | =end original | |
851 | ||
852 | サブルーチンと同様、スレッドのエントリポイント関数から返される値の型は | |
853 | スレッドの I<コンテキスト> (リスト、スカラ、無効のいずれか) によって | |
854 | 決定されます。 | |
855 | スレッドのコンテキストはスレッド作成時に決定されます。 | |
856 | これは、コンテキストをエントリポイント関数から | |
857 | L<wantarray()|perlfunc/"wantarray"> を使って利用可能にするために必要です。 | |
858 | それからスレッドは C<-E<gt>join()> から返される適切な型の値を指定します。 | |
859 | ||
860 | =head2 Explicit context | |
861 | ||
862 | (明示的なコンテキスト) | |
863 | ||
864 | =begin original | |
865 | ||
866 | Because thread creation and thread joining may occur in different contexts, it | |
867 | may be desirable to state the context explicitly to the thread's entry point | |
868 | function. This may be done by calling C<-E<gt>create()> with a hash reference | |
869 | as the first argument: | |
870 | ||
871 | =end original | |
872 | ||
873 | スレッドの作成とスレッドの join は異なったコンテキストで | |
874 | 行われるかもしれないので、スレッドのエントリポイント関数で明示的に | |
875 | コンテキストを宣言することが望ましいです。 | |
876 | これは最初の引数としてハッシュリファレンスを指定した C<-E<gt>create()> を | |
877 | 呼び出すことで行えます: | |
878 | ||
879 | my $thr = threads->create({'context' => 'list'}, \&foo); | |
880 | ... | |
881 | my @results = $thr->join(); | |
882 | ||
883 | =begin original | |
884 | ||
885 | In the above, the threads object is returned to the parent thread in scalar | |
886 | context, and the thread's entry point function C<foo> will be called in list | |
887 | (array) context such that the parent thread can receive a list (array) from | |
888 | the C<-E<gt>join()> call. (C<'array'> is synonymous with C<'list'>.) | |
889 | ||
890 | =end original | |
891 | ||
892 | 上述の場合、スレッドオブジェクトは親スレッドにスカラコンテキストで | |
893 | 返され、スレッドのエントリポイント関数 C<foo> はリスト(配列)コンテキストで | |
894 | 予備されるので、親スレッドは C<-E<gt>join()> 呼び出しからリスト(配列)を | |
895 | 受け取ります。 | |
896 | (C<'array'> は C<'list'> の同義語です。) | |
897 | ||
898 | =begin original | |
899 | ||
900 | Similarly, if you need the threads object, but your thread will not be | |
901 | returning a value (i.e., I<void> context), you would do the following: | |
902 | ||
903 | =end original | |
904 | ||
905 | 同様に、もしスレッドオブジェクトが必要だけれども、スレッドが値を返さない | |
906 | (つまり I<無効> コンテキスト) 場合、以下のようにします: | |
907 | ||
908 | my $thr = threads->create({'context' => 'void'}, \&foo); | |
909 | ... | |
910 | $thr->join(); | |
911 | ||
912 | =begin original | |
913 | ||
914 | The context type may also be used as the I<key> in the hash reference followed | |
915 | by a I<true> value: | |
916 | ||
917 | =end original | |
918 | ||
919 | コンテキスト型はまた、ハッシュリファレンスの I<キー> に引き続いて I<真> の | |
920 | 値としても使えます: | |
921 | ||
922 | threads->create({'scalar' => 1}, \&foo); | |
923 | ... | |
924 | my ($thr) = threads->list(); | |
925 | my $result = $thr->join(); | |
926 | ||
927 | =head2 Implicit context | |
928 | ||
929 | (暗黙のコンテキスト) | |
930 | ||
931 | =begin original | |
932 | ||
933 | If not explicitly stated, the thread's context is implied from the context | |
934 | of the C<-E<gt>create()> call: | |
935 | ||
936 | =end original | |
937 | ||
938 | 明示的に宣言されない場合、スレッドのコンテキストは C<-E<gt>create()> | |
939 | 呼び出しのコンテキストになります: | |
940 | ||
941 | # Create thread in list context | |
942 | my ($thr) = threads->create(...); | |
943 | ||
944 | # Create thread in scalar context | |
945 | my $thr = threads->create(...); | |
946 | ||
947 | # Create thread in void context | |
948 | threads->create(...); | |
949 | ||
950 | =head2 $thr->wantarray() | |
951 | ||
952 | =begin original | |
953 | ||
954 | This returns the thread's context in the same manner as | |
955 | L<wantarray()|perlfunc/"wantarray">. | |
956 | ||
957 | =end original | |
958 | ||
959 | これは L<wantarray()|perlfunc/"wantarray"> と同じ方法でスレッドの | |
960 | コンテキストを返します。 | |
961 | ||
962 | =head2 threads->wantarray() | |
963 | ||
964 | =begin original | |
965 | ||
966 | Class method to return the current thread's context. This returns the same | |
967 | value as running L<wantarray()|perlfunc/"wantarray"> inside the current | |
968 | thread's entry point function. | |
969 | ||
970 | =end original | |
971 | ||
972 | 現在のスレッドのコンテキストを返すクラスメソッドです。 | |
973 | 現在のスレッドのエントリポイント関数の内側で | |
974 | L<wantarray()|perlfunc/"wantarray"> を実行するのと同じ値を返します。 | |
975 | ||
976 | =head1 THREAD STACK SIZE | |
977 | ||
978 | (スレッドのスタックサイズ) | |
979 | ||
980 | =begin original | |
981 | ||
982 | The default per-thread stack size for different platforms varies | |
983 | significantly, and is almost always far more than is needed for most | |
984 | applications. On Win32, Perl's makefile explicitly sets the default stack to | |
985 | 16 MB; on most other platforms, the system default is used, which again may be | |
986 | much larger than is needed. | |
987 | ||
988 | =end original | |
989 | ||
990 | デフォルトのスレッド毎のスタックサイズはプラットフォームによって大きく異なり、 | |
991 | ほとんど常にほとんどのアプリケーションが必要な量よりはるかに多いです。 | |
992 | Win32 では、Perl の makefile は明示的にデフォルトのスタックを 16 MB に | |
993 | 指定しています; その他のほとんどのシステムでは、システムのデフォルトが | |
994 | 使われますが、やはり必要な量よりはるかに多いです。 | |
995 | ||
996 | =begin original | |
997 | ||
998 | By tuning the stack size to more accurately reflect your application's needs, | |
999 | you may significantly reduce your application's memory usage, and increase the | |
1000 | number of simultaneously running threads. | |
1001 | ||
1002 | =end original | |
1003 | ||
1004 | スタックサイズをアプリケーションのニーズにより正確に反映させることにより、 | |
1005 | アプリケーションのメモリ使用量を著しく減少させ、同時実行スレッド数を | |
1006 | 増やすことができるかもしれません。 | |
1007 | ||
1008 | =begin original | |
1009 | ||
1010 | Note that on Windows, address space allocation granularity is 64 KB, | |
1011 | therefore, setting the stack smaller than that on Win32 Perl will not save any | |
1012 | more memory. | |
1013 | ||
1014 | =end original | |
1015 | ||
1016 | 従って、アドレス空間配置の粒度が 64 KB である Windows では、Win32 Perl で | |
1017 | これより小さい値にスタックを設定してもメモリを節約できないことに | |
1018 | 注意してください。 | |
1019 | ||
1020 | =over | |
1021 | ||
1022 | =item threads->get_stack_size(); | |
1023 | ||
1024 | =begin original | |
1025 | ||
1026 | Returns the current default per-thread stack size. The default is zero, which | |
1027 | means the system default stack size is currently in use. | |
1028 | ||
1029 | =end original | |
1030 | ||
1031 | 現在のデフォルトのスレッド毎のスタックサイズを返します。 | |
1032 | デフォルトは 0 で、これはシステムのデフォルトスタックサイズを | |
1033 | 使っていることを示します。 | |
1034 | ||
1035 | =item $size = $thr->get_stack_size(); | |
1036 | ||
1037 | =begin original | |
1038 | ||
1039 | Returns the stack size for a particular thread. A return value of zero | |
1040 | indicates the system default stack size was used for the thread. | |
1041 | ||
1042 | =end original | |
1043 | ||
1044 | 特定のスレッドのスタックサイズを返します。 | |
1045 | 返り値 0 は、そのスレッドでシステムデフォルトのスタックサイズが | |
1046 | 使われていることを示します。 | |
1047 | ||
1048 | =item $old_size = threads->set_stack_size($new_size); | |
1049 | ||
1050 | =begin original | |
1051 | ||
1052 | Sets a new default per-thread stack size, and returns the previous setting. | |
1053 | ||
1054 | =end original | |
1055 | ||
1056 | 新しいデフォルトのスレッド毎のスタックサイズを設定し、以前の設定を | |
1057 | 返します。 | |
1058 | ||
1059 | =begin original | |
1060 | ||
1061 | Some platforms have a minimum thread stack size. Trying to set the stack size | |
1062 | below this value will result in a warning, and the minimum stack size will be | |
1063 | used. | |
1064 | ||
1065 | =end original | |
1066 | ||
1067 | 最小スレッドスタックサイズがあるプラットフォームもあります。 | |
1068 | その値よりスタックサイズを小さくしようとすると警告が出て、最小 | |
1069 | スタックサイズが使われます。 | |
1070 | ||
1071 | =begin original | |
1072 | ||
1073 | Some Linux platforms have a maximum stack size. Setting too large of a stack | |
1074 | size will cause thread creation to fail. | |
1075 | ||
1076 | =end original | |
1077 | ||
1078 | 最大スタックサイズのある Linux プラットフォームもあります。 | |
1079 | 大きすぎるスタックサイズを設定するとスレッド作成に失敗します。 | |
1080 | ||
1081 | =begin original | |
1082 | ||
1083 | If needed, C<$new_size> will be rounded up to the next multiple of the memory | |
1084 | page size (usually 4096 or 8192). | |
1085 | ||
1086 | =end original | |
1087 | ||
1088 | 必要なら、C<$new_size> は次のメモリページサイズ(普通は4096 か 8192)倍数に | |
1089 | 切り上げられます。 | |
1090 | ||
1091 | =begin original | |
1092 | ||
1093 | Threads created after the stack size is set will then either call | |
1094 | C<pthread_attr_setstacksize()> I<(for pthreads platforms)>, or supply the | |
1095 | stack size to C<CreateThread()> I<(for Win32 Perl)>. | |
1096 | ||
1097 | =end original | |
1098 | ||
1099 | スタックサイズが設定された後に作られたスレッドは | |
1100 | C<pthread_attr_setstacksize()> を呼び出す I<(pthreads プラットフォームの | |
1101 | 場合)> か C<CreateThread()> にスタックサイズを渡します | |
1102 | I<(Win32 Perl の場合)>。 | |
1103 | ||
1104 | =begin original | |
1105 | ||
1106 | (Obviously, this call does not affect any currently extant threads.) | |
1107 | ||
1108 | =end original | |
1109 | ||
1110 | (明らかに、この呼び出しは既に存在するスレッドには影響を与えません。) | |
1111 | ||
1112 | =item use threads ('stack_size' => VALUE); | |
1113 | ||
1114 | =begin original | |
1115 | ||
1116 | This sets the default per-thread stack size at the start of the application. | |
1117 | ||
1118 | =end original | |
1119 | ||
1120 | これはアプリケーションの開始時にスタック単位のデフォルトのスタックサイズを | |
1121 | 設定します。 | |
1122 | ||
1123 | =item $ENV{'PERL5_ITHREADS_STACK_SIZE'} | |
1124 | ||
1125 | =begin original | |
1126 | ||
1127 | The default per-thread stack size may be set at the start of the application | |
1128 | through the use of the environment variable C<PERL5_ITHREADS_STACK_SIZE>: | |
1129 | ||
1130 | =end original | |
1131 | ||
1132 | デフォルトのスレッド毎のスタックサイズは環境変数 | |
1133 | C<PERL5_ITHREADS_STACK_SIZE> を使ってアプリケーションの開始時に設定できます: | |
1134 | ||
1135 | PERL5_ITHREADS_STACK_SIZE=1048576 | |
1136 | export PERL5_ITHREADS_STACK_SIZE | |
1137 | perl -e'use threads; print(threads->get_stack_size(), "\n")' | |
1138 | ||
1139 | =begin original | |
1140 | ||
1141 | This value overrides any C<stack_size> parameter given to C<use threads>. Its | |
1142 | primary purpose is to permit setting the per-thread stack size for legacy | |
1143 | threaded applications. | |
1144 | ||
1145 | =end original | |
1146 | ||
1147 | この値は C<use threads> に与えられる C<stack_size> 引数で上書きできます。 | |
1148 | 主な目的はレガシーなスレッドアプリケーションでスレッド毎のスタックサイズを | |
1149 | 設定できるようにすることです。 | |
1150 | ||
1151 | =item threads->create({'stack_size' => VALUE}, FUNCTION, ARGS) | |
1152 | ||
1153 | =begin original | |
1154 | ||
1155 | To specify a particular stack size for any individual thread, call | |
1156 | C<-E<gt>create()> with a hash reference as the first argument: | |
1157 | ||
1158 | =end original | |
1159 | ||
1160 | 個々のスレッドの個別のスタックサイズを指定するには、最初の引数として | |
1161 | ハッシュリファレンスを指定して C<-E<gt>create()> を呼び出します: | |
1162 | ||
1163 | my $thr = threads->create({'stack_size' => 32*4096}, \&foo, @args); | |
1164 | ||
1165 | =item $thr2 = $thr1->create(FUNCTION, ARGS) | |
1166 | ||
1167 | =begin original | |
1168 | ||
1169 | This creates a new thread (C<$thr2>) that inherits the stack size from an | |
1170 | existing thread (C<$thr1>). This is shorthand for the following: | |
1171 | ||
1172 | =end original | |
1173 | ||
1174 | これは既に存在するスレッド (C<$thr1>) からスタックサイズを継承して新しい | |
1175 | スレッド (C<$thr2>) を作成します。 | |
1176 | これは以下のものの短縮形です: | |
1177 | ||
1178 | my $stack_size = $thr1->get_stack_size(); | |
1179 | my $thr2 = threads->create({'stack_size' => $stack_size}, FUNCTION, ARGS); | |
1180 | ||
1181 | =back | |
1182 | ||
1183 | =head1 THREAD SIGNALLING | |
1184 | ||
1185 | (スレッドとシグナル) | |
1186 | ||
1187 | =begin original | |
1188 | ||
1189 | When safe signals is in effect (the default behavior - see L</"Unsafe signals"> | |
1190 | for more details), then signals may be sent and acted upon by individual | |
1191 | threads. | |
1192 | ||
1193 | =end original | |
1194 | ||
1195 | 安全なシグナルが有効なとき (デフォルトの振る舞いです - さらなる | |
1196 | 詳細については L</"Unsafe signals"> を参照してください)、シグナルは | |
1197 | それぞれのスレッドに対して送られて動作します。 | |
1198 | ||
1199 | 173 | =over 4 |
1200 | 174 | |
1201 | =item | |
175 | =item A thread exited while %d other threads were still running | |
1202 | 176 | |
1203 | ||
177 | A thread (not necessarily the main thread) exited while there were | |
178 | still other threads running. Usually it's a good idea to first collect | |
179 | the return values of the created threads by joining them, and only then | |
180 | exit from the main thread. | |
1204 | 181 | |
1205 | ||
182 | あるスレッド(メインスレッドである必要はない)が、まだ他のスレッドが実行中にexitした。通常、はじめに生成されたスレッドの戻り値をjoinでもって回収し、それからメインスレッドからexitするのがよい方法だ。 | |
1206 | numbers are the same as those supported by | |
1207 | L<kill()|perlfunc/"kill SIGNAL, LIST">. For example, 'SIGTERM', 'TERM' and | |
1208 | (depending on the OS) 15 are all valid arguments to C<-E<gt>kill()>. | |
1209 | 183 | |
1210 | =end original | |
1211 | ||
1212 | 指定されたシグナルをスレッドに送ります。 | |
1213 | シグナル名と(正の)シグナル番号は L<kill()|perlfunc/"kill SIGNAL, LIST"> で | |
1214 | 対応しているものと同じです。 | |
1215 | 例えば、'SIGTERM', 'TERM' と (OS に依存しますが) 15 は全て | |
1216 | C<-E<gt>kill()> への妥当な引数です。 | |
1217 | ||
1218 | =begin original | |
1219 | ||
1220 | Returns the thread object to allow for method chaining: | |
1221 | ||
1222 | =end original | |
1223 | ||
1224 | メソッドチェーンができるように、スレッドオブジェクトを返します: | |
1225 | ||
1226 | $thr->kill('SIG...')->join(); | |
1227 | ||
1228 | 184 | =back |
1229 | 185 | |
1230 | = | |
186 | =head1 TODO | |
1231 | 187 | |
1232 | ||
188 | The current implementation of threads has been an attempt to get | |
1233 | ||
189 | a correct threading system working that could be built on, | |
190 | and optimized, in newer versions of perl. | |
1234 | 191 | |
1235 | ||
192 | 現在のスレッド実装は、より新しいPerlのバージョンにおいて、 | |
193 | 正しく動作するスレッドシステムとして構築され、最適化されてきた。 | |
1236 | 194 | |
1237 | ||
195 | Currently the overhead of creating a thread is rather large, | |
1238 | ||
196 | also the cost of returning values can be large. These are areas | |
1239 | ||
197 | were there most likely will be work done to optimize what data | |
198 | that needs to be cloned. | |
1240 | 199 | |
1241 | ||
200 | 現在のところ、スレッドの生成にかかるオーバーヘッドはやや大きく、 | |
201 | また、値を返すためのコストも大きくなりかねない。クローンされる | |
202 | 必要のあるデータが何なのかを最適化する余地があるだろう。 | |
1242 | 203 | |
1243 | ||
204 | =head1 バグ | |
1244 | { | |
1245 | # Thread 'cancellation' signal handler | |
1246 | $SIG{'KILL'} = sub { threads->exit(); }; | |
1247 | 205 | |
1248 | ... | |
1249 | } | |
1250 | ||
1251 | # Create a thread | |
1252 | my $thr = threads->create('thr_func'); | |
1253 | ||
1254 | ... | |
1255 | ||
1256 | # Signal the thread to terminate, and then detach | |
1257 | # it so that it will get cleaned up automatically | |
1258 | $thr->kill('KILL')->detach(); | |
1259 | ||
1260 | =begin original | |
1261 | ||
1262 | Here's another simplistic example that illustrates the use of thread | |
1263 | signalling in conjunction with a semaphore to provide rudimentary I<suspend> | |
1264 | and I<resume> capabilities: | |
1265 | ||
1266 | =end original | |
1267 | ||
1268 | 以下は基本的な I<中断> と I<再開> の機能を提供するためにスレッドの | |
1269 | シグナルをセマフォと組み合わせた使い方を示すための単純化されたもう一つの | |
1270 | 例です: | |
1271 | ||
1272 | use threads; | |
1273 | use Thread::Semaphore; | |
1274 | ||
1275 | sub thr_func | |
1276 | { | |
1277 | my $sema = shift; | |
1278 | ||
1279 | # Thread 'suspend/resume' signal handler | |
1280 | $SIG{'STOP'} = sub { | |
1281 | $sema->down(); # Thread suspended | |
1282 | $sema->up(); # Thread resumes | |
1283 | }; | |
1284 | ||
1285 | ... | |
1286 | } | |
1287 | ||
1288 | # Create a semaphore and pass it to a thread | |
1289 | my $sema = Thread::Semaphore->new(); | |
1290 | my $thr = threads->create('thr_func', $sema); | |
1291 | ||
1292 | # Suspend the thread | |
1293 | $sema->down(); | |
1294 | $thr->kill('STOP'); | |
1295 | ||
1296 | ... | |
1297 | ||
1298 | # Allow the thread to continue | |
1299 | $sema->up(); | |
1300 | ||
1301 | =begin original | |
1302 | ||
1303 | CAVEAT: The thread signalling capability provided by this module does not | |
1304 | actually send signals via the OS. It I<emulates> signals at the Perl-level | |
1305 | such that signal handlers are called in the appropriate thread. For example, | |
1306 | sending C<$thr-E<gt>kill('STOP')> does not actually suspend a thread (or the | |
1307 | whole process), but does cause a C<$SIG{'STOP'}> handler to be called in that | |
1308 | thread (as illustrated above). | |
1309 | ||
1310 | =end original | |
1311 | ||
1312 | 警告: このモジュールによって提供されているスレッドへのシグナル機能は | |
1313 | 実際には OS 経由でシグナルを送っていません。 | |
1314 | シグナルハンドラが適切なスレッドで呼び出されるように Perl レベルでシグナルを | |
1315 | I<エミュレート> しています。 | |
1316 | 例えば、C<$thr-E<gt>kill('STOP')> は実際にはスレッド(またはプロセス全体)を | |
1317 | 停止させませんが、(上述したように) 対象のスレッドの C<$SIG{'STOP'}> | |
1318 | ハンドラが呼び出されます。 | |
1319 | ||
1320 | =begin original | |
1321 | ||
1322 | As such, signals that would normally not be appropriate to use in the | |
1323 | C<kill()> command (e.g., C<kill('KILL', $$)>) are okay to use with the | |
1324 | C<-E<gt>kill()> method (again, as illustrated above). | |
1325 | ||
1326 | =end original | |
1327 | ||
1328 | そのため、普通は C<kill()> コマンドでの使用が適切ではないようなシグナル | |
1329 | (例えば C<kill('KILL', $$)>)) は C<-E<gt>kill()> メソッドで使っても | |
1330 | (再び上述したように)問題ありません。 | |
1331 | ||
1332 | =begin original | |
1333 | ||
1334 | Correspondingly, sending a signal to a thread does not disrupt the operation | |
1335 | the thread is currently working on: The signal will be acted upon after the | |
1336 | current operation has completed. For instance, if the thread is I<stuck> on | |
1337 | an I/O call, sending it a signal will not cause the I/O call to be interrupted | |
1338 | such that the signal is acted up immediately. | |
1339 | ||
1340 | =end original | |
1341 | ||
1342 | 同様に、スレッドにシグナルを送ってもスレッドが今処理している操作を | |
1343 | 妨害しません: シグナルは現在の処理が終了した後に処理されます。 | |
1344 | 例えば、スレッドが I/O 呼び出して I<固まっている> なら、シグナルが直ちに | |
1345 | 処理されるように I/O 呼び出しを中断はしません。 | |
1346 | ||
1347 | =begin original | |
1348 | ||
1349 | Sending a signal to a terminated thread is ignored. | |
1350 | ||
1351 | =end original | |
1352 | ||
1353 | 終了したスレッドへのシグナル送信は無視されます。 | |
1354 | ||
1355 | =head1 WARNINGS | |
1356 | ||
1357 | (警告) | |
1358 | ||
1359 | =over 4 | |
1360 | ||
1361 | =item Perl exited with active threads: | |
1362 | ||
1363 | =begin original | |
1364 | ||
1365 | If the program exits without all threads having either been joined or | |
1366 | detached, then this warning will be issued. | |
1367 | ||
1368 | =end original | |
1369 | ||
1370 | 全てのスレッドが join されるか detach される前にプログラムが終了した場合、 | |
1371 | この警告が発生します。 | |
1372 | ||
1373 | =begin original | |
1374 | ||
1375 | NOTE: If the I<main> thread exits, then this warning cannot be suppressed | |
1376 | using C<no warnings 'threads';> as suggested below. | |
1377 | ||
1378 | =end original | |
1379 | ||
1380 | 注意: I<main> スレッドが存在しているなら、後に示唆しているようにこの警告は | |
1381 | C<no warnings 'threads';> を使って抑制できません。 | |
1382 | ||
1383 | =item Thread creation failed: pthread_create returned # | |
1384 | ||
1385 | =begin original | |
1386 | ||
1387 | See the appropriate I<man> page for C<pthread_create> to determine the actual | |
1388 | cause for the failure. | |
1389 | ||
1390 | =end original | |
1391 | ||
1392 | 失敗の実際の原因を決定するには C<pthread_create> の適切な I<man> ページを | |
1393 | 参照してください。 | |
1394 | ||
1395 | =item Thread # terminated abnormally: ... | |
1396 | ||
1397 | =begin original | |
1398 | ||
1399 | A thread terminated in some manner other than just returning from its entry | |
1400 | point function, or by using C<threads-E<gt>exit()>. For example, the thread | |
1401 | may have terminated because of an error, or by using C<die>. | |
1402 | ||
1403 | =end original | |
1404 | ||
1405 | スレッドが単にエントリポイント関数から返ったか C<threads-E<gt>exit()> を | |
1406 | 使った以外の何らかの方法で終了しました。 | |
1407 | 例えば、エラーや C<die> の使用によってスレッドが終了しました。 | |
1408 | ||
1409 | =item Using minimum thread stack size of # | |
1410 | ||
1411 | =begin original | |
1412 | ||
1413 | Some platforms have a minimum thread stack size. Trying to set the stack size | |
1414 | below this value will result in the above warning, and the stack size will be | |
1415 | set to the minimum. | |
1416 | ||
1417 | =end original | |
1418 | ||
1419 | 最低スレッドスタックサイズがあるプラットフォームもあります。 | |
1420 | スタックサイズをその値以下に設定しようとするとこの警告が出て、 | |
1421 | スタックサイズは最小値に設定されます。 | |
1422 | ||
1423 | =item Thread creation failed: pthread_attr_setstacksize(I<SIZE>) returned 22 | |
1424 | ||
1425 | =begin original | |
1426 | ||
1427 | The specified I<SIZE> exceeds the system's maximum stack size. Use a smaller | |
1428 | value for the stack size. | |
1429 | ||
1430 | =end original | |
1431 | ||
1432 | 指定された I<SIZE> がシステムの最大スタックサイズを超えています。 | |
1433 | スタックサイズとしてより小さい値を使ってください。 | |
1434 | ||
1435 | =back | |
1436 | ||
1437 | =begin original | |
1438 | ||
1439 | If needed, thread warnings can be suppressed by using: | |
1440 | ||
1441 | =end original | |
1442 | ||
1443 | もし必要なら、スレッドの警告は以下のものを: | |
1444 | ||
1445 | no warnings 'threads'; | |
1446 | ||
1447 | =begin original | |
1448 | ||
1449 | in the appropriate scope. | |
1450 | ||
1451 | =end original | |
1452 | ||
1453 | 適切なスコープで使うことで抑制できます。 | |
1454 | ||
1455 | =head1 ERRORS | |
1456 | ||
1457 | (エラー) | |
1458 | ||
1459 | =over 4 | |
1460 | ||
1461 | =item This Perl not built to support threads | |
1462 | ||
1463 | =begin original | |
1464 | ||
1465 | The particular copy of Perl that you're trying to use was not built using the | |
1466 | C<useithreads> configuration option. | |
1467 | ||
1468 | =end original | |
1469 | ||
1470 | 使おうとしている Perl が C<useithreads> 設定オプションを使って | |
1471 | ビルドされていません。 | |
1472 | ||
1473 | =begin original | |
1474 | ||
1475 | Having threads support requires all of Perl and all of the XS modules in the | |
1476 | Perl installation to be rebuilt; it is not just a question of adding the | |
1477 | L<threads> module (i.e., threaded and non-threaded Perls are binary | |
1478 | incompatible.) | |
1479 | ||
1480 | =end original | |
1481 | ||
1482 | スレッド対応にするためには Perl の全てと Perl インストールの全ての XS | |
1483 | モジュールを再ビルドする必要があります; これは L<threads> モジュールを | |
1484 | 追加するためだけではありません (つまり、スレッド対応 Perl と非対応 Perl は | |
1485 | バイナリ互換性がありません。) | |
1486 | ||
1487 | =item Cannot change stack size of an existing thread | |
1488 | ||
1489 | =begin original | |
1490 | ||
1491 | The stack size of currently extant threads cannot be changed, therefore, the | |
1492 | following results in the above error: | |
1493 | ||
1494 | =end original | |
1495 | ||
1496 | 現在既にあるスレッドのスタックサイズは変更できないので、以下のようなものは | |
1497 | 上述のエラーになります: | |
1498 | ||
1499 | $thr->set_stack_size($size); | |
1500 | ||
1501 | =item Cannot signal threads without safe signals | |
1502 | ||
1503 | =begin original | |
1504 | ||
1505 | Safe signals must be in effect to use the C<-E<gt>kill()> signalling method. | |
1506 | See L</"Unsafe signals"> for more details. | |
1507 | ||
1508 | =end original | |
1509 | ||
1510 | C<-E<gt>kill()> シグナルメソッドを使うには安全なシグナルが | |
1511 | 有効でなければなりません。 | |
1512 | さらなる詳細については L</"Unsafe signals"> を参照してください。 | |
1513 | ||
1514 | =item Unrecognized signal name: ... | |
1515 | ||
1516 | =begin original | |
1517 | ||
1518 | The particular copy of Perl that you're trying to use does not support the | |
1519 | specified signal being used in a C<-E<gt>kill()> call. | |
1520 | ||
1521 | =end original | |
1522 | ||
1523 | 使おうとしている Perl が C<-E<gt>kill()> 呼び出しで使おうとしているシグナルに | |
1524 | 対応していません。 | |
1525 | ||
1526 | =back | |
1527 | ||
1528 | =head1 BUGS AND LIMITATIONS | |
1529 | ||
1530 | (バグと制限) | |
1531 | ||
1532 | =begin original | |
1533 | ||
1534 | Before you consider posting a bug report, please consult, and possibly post a | |
1535 | message to the discussion forum to see if what you've encountered is a known | |
1536 | problem. | |
1537 | ||
1538 | =end original | |
1539 | ||
1540 | バグ報告を投稿することを考える前に、まず相談してください; そしてできれば | |
1541 | 遭遇したものが既知の問題かどうかを見るために議論フォーラムにメッセージを | |
1542 | 投稿してください。 | |
1543 | ||
1544 | 206 | =over |
1545 | 207 | |
1546 | =item | |
208 | =item 親-子 スレッド. | |
1547 | 209 | |
1548 | ||
210 | On some platforms it might not be possible to destroy "parent" | |
211 | threads while there are still existing child "threads". | |
1549 | 212 | |
1550 | ||
213 | プラットフォームによっては、子スレッドがまだ存在している間は | |
214 | 親スレッドを破壊することができないことがある。 | |
1551 | 215 | |
1552 | ||
216 | This will possibly be fixed in later versions of perl. | |
1553 | be used in threaded applications, especially if those modules use non-Perl | |
1554 | data, or XS code. | |
1555 | 217 | |
1556 | ||
218 | たぶん今後のperlのバージョンではフィックスされるだろう。 | |
1557 | 219 | |
1558 | ||
220 | =item tidはI32 | |
1559 | とくにモジュールが非 Perl データや XS コードを使っているときは、 | |
1560 | L<perlmod/"Making your module threadsafe"> を参照してください。 | |
1561 | 221 | |
1562 | ||
222 | The thread id is a 32 bit integer, it can potentially overflow. | |
223 | This might be fixed in a later version of perl. | |
1563 | 224 | |
1564 | ||
225 | スレッドidは32bit整数である。オーバーフローする可能性がある。 | |
226 | 今後のperlのバージョンではフィックスされるだろう。 | |
1565 | 227 | |
1566 | = | |
228 | =item オブジェクトの戻り | |
1567 | 229 | |
1568 | ||
230 | When you return an object the entire stash that the object is blessed | |
1569 | ||
231 | as well. This will lead to a large memory usage. The ideal situation | |
1570 | ||
232 | would be to detect the original stash if it existed. | |
1571 | application, it may be possible to work around such difficulties. | |
1572 | 233 | |
1573 | ||
234 | オブジェクトを返すとき、そのオブジェクトがblessされているstashも | |
235 | 同様である。これはメモリを大量に使用するかもしれない。 | |
236 | 理想的な状態は、存在しているならオリジナルのstashを見つけることなのだが。 | |
1574 | 237 | |
1575 | ||
238 | =item BEGINブロック内でのスレッドの生成 | |
1576 | 遭遇するかもしれません。 | |
1577 | 例えば、実行中に Perl インタプリタがクラッシュしたり、コアダンプして | |
1578 | 終了したりするかもしれません。 | |
1579 | モジュールとアプリケーションの必要事項に依存して、このような問題を | |
1580 | 回避できることがあります。 | |
1581 | 239 | |
1582 | ||
240 | Creating threads inside BEGIN blocks (or during the compilation phase | |
241 | in general) does not work. (In Windows, trying to use fork() inside | |
242 | BEGIN blocks is an equally losing proposition, since it has been | |
243 | implemented in very much the same way as threads.) | |
1583 | 244 | |
1584 | I | |
245 | BEGINブロック内で(つまり一般的にコンパイルフェーズの段階で) | |
1585 | ||
246 | スレッドを生成することはできない(WindowsのBEGINブロック内でfork()を利用しようとする試みも同様に見込みのない提案である。なぜならスレッドと全く同じ方法で実装されてきたからだ)。 | |
1586 | C<import> if needed): | |
1587 | 247 | |
1588 | =e | |
248 | =item PERL_OLD_SIGNALSはスレッドセーフではない。これからも。 | |
1589 | 249 | |
1590 | ||
250 | If your Perl has been built with PERL_OLD_SIGNALS (one has | |
1591 | ||
251 | to explicitly add that symbol to ccflags, see C<perl -V>), | |
1592 | ||
252 | signal handling is not threadsafe. | |
1593 | 253 | |
1594 | ||
254 | もしあなたがPERL_OLD_SIGNALSオプションを有効にしてPerlをbuiltしてるなら(このシンボルをccflagに明示的に追加しなければならない。C<perl -V>を見よ)、シグナルハンドリングはスレッドセーフではない。 | |
1595 | { | |
1596 | require Unsafe::Module | |
1597 | # Unsafe::Module->import(...); | |
1598 | 255 | |
1599 | .... | |
1600 | } | |
1601 | ||
1602 | =begin original | |
1603 | ||
1604 | If the module is needed inside the I<main> thread, try modifying your | |
1605 | application so that the module is loaded (again using C<require> and | |
1606 | C<-E<gt>import()>) after any threads are started, and in such a way that no | |
1607 | other threads are started afterwards. | |
1608 | ||
1609 | =end original | |
1610 | ||
1611 | モジュールが I<main> スレッドの内側で必要なら、スレッドを開始してから | |
1612 | (再び C<require> と C<-E<gt>import()> を使って) モジュールが | |
1613 | 読み込まれるように、そしてその後他のスレッドが開始しないように | |
1614 | アプリケーションを修正してみてください。 | |
1615 | ||
1616 | =begin original | |
1617 | ||
1618 | If the above does not work, or is not adequate for your application, then file | |
1619 | a bug report on L<http://rt.cpan.org/Public/> against the problematic module. | |
1620 | ||
1621 | =end original | |
1622 | ||
1623 | 上述のものが動作しないか、アプリケーションに適切でないなら、問題のある | |
1624 | モジュールに対して L<http://rt.cpan.org/Public/> にバグ報告を | |
1625 | 登録してください。 | |
1626 | ||
1627 | =item Current working directory | |
1628 | ||
1629 | (カレントワーキングディレクトリ) | |
1630 | ||
1631 | =begin original | |
1632 | ||
1633 | On all platforms except MSWin32, the setting for the current working directory | |
1634 | is shared among all threads such that changing it in one thread (e.g., using | |
1635 | C<chdir()>) will affect all the threads in the application. | |
1636 | ||
1637 | =end original | |
1638 | ||
1639 | MSWin32 以外の全てのプラットフォームでは、カレントワーキングディレクトリの | |
1640 | 設定は全てのスレッドで共有されるので、あるスレッドでこれを変更する | |
1641 | (つまり C<chdir()> を使う) とアプリケーションの全てのスレッドに | |
1642 | 影響します。 | |
1643 | ||
1644 | =begin original | |
1645 | ||
1646 | On MSWin32, each thread maintains its own the current working directory | |
1647 | setting. | |
1648 | ||
1649 | =end original | |
1650 | ||
1651 | MSWin32 では、それぞれのカレントワーキングディレクトリ設定を独自に | |
1652 | 管理しています。 | |
1653 | ||
1654 | =item Environment variables | |
1655 | ||
1656 | (環境変数) | |
1657 | ||
1658 | =begin original | |
1659 | ||
1660 | Currently, on all platforms except MSWin32, all I<system> calls (e.g., using | |
1661 | C<system()> or back-ticks) made from threads use the environment variable | |
1662 | settings from the I<main> thread. In other words, changes made to C<%ENV> in | |
1663 | a thread will not be visible in I<system> calls made by that thread. | |
1664 | ||
1665 | =end original | |
1666 | ||
1667 | 現在のところ、MSWin32 以外の全てのプラットフォームでは、 | |
1668 | スレッドによって作られた (C<system()> または逆クォートによる) 全ての | |
1669 | I<system> 呼び出しは I<main> スレッドの環境変数設定を使います。 | |
1670 | 言い換えると、スレッドで行った C<%ENV> への変更は、そのスレッドで作られた | |
1671 | I<system> 呼び出しでは見えません。 | |
1672 | ||
1673 | =begin original | |
1674 | ||
1675 | To work around this, set environment variables as part of the I<system> call. | |
1676 | For example: | |
1677 | ||
1678 | =end original | |
1679 | ||
1680 | これを回避するには、I<system> 呼び出しの一部として環境変数をセットします。 | |
1681 | 例えば: | |
1682 | ||
1683 | my $msg = 'hello'; | |
1684 | system("FOO=$msg; echo \$FOO"); # Outputs 'hello' to STDOUT | |
1685 | ||
1686 | =begin original | |
1687 | ||
1688 | On MSWin32, each thread maintains its own set of environment variables. | |
1689 | ||
1690 | =end original | |
1691 | ||
1692 | MSWin32 では、各スレッドでは独自の環境変数集合を管理します。 | |
1693 | ||
1694 | =item Parent-child threads | |
1695 | ||
1696 | (親-子スレッド) | |
1697 | ||
1698 | =begin original | |
1699 | ||
1700 | On some platforms, it might not be possible to destroy I<parent> threads while | |
1701 | there are still existing I<child> threads. | |
1702 | ||
1703 | =end original | |
1704 | ||
1705 | プラットフォームによっては、I<子> スレッドがまだ存在している間は | |
1706 | I<親> スレッドを破壊することができないことがあります。 | |
1707 | ||
1708 | =item Creating threads inside special blocks | |
1709 | ||
1710 | (特殊ブロックの中でスレッドを作る) | |
1711 | ||
1712 | =begin original | |
1713 | ||
1714 | Creating threads inside C<BEGIN>, C<CHECK> or C<INIT> blocks should not be | |
1715 | relied upon. Depending on the Perl version and the application code, results | |
1716 | may range from success, to (apparently harmless) warnings of leaked scalar, or | |
1717 | all the way up to crashing of the Perl interpreter. | |
1718 | ||
1719 | =end original | |
1720 | ||
1721 | C<BEGIN>, C<CHECK>, C<INIT> ブロックの内側でスレッドを作成することを | |
1722 | 信頼するべきではありません。 | |
1723 | Perl バージョンとアプリケーションコードに依存して、成功から(おそらくは | |
1724 | 無害な)リークしたスカラの警告、Perl インタプリタのクラッシュまでさまざまな | |
1725 | 結果となります。 | |
1726 | ||
1727 | =item Unsafe signals | |
1728 | ||
1729 | (安全でないシグナル) | |
1730 | ||
1731 | =begin original | |
1732 | ||
1733 | Since Perl 5.8.0, signals have been made safer in Perl by postponing their | |
1734 | handling until the interpreter is in a I<safe> state. See | |
1735 | L<perl58delta/"Safe Signals"> and L<perlipc/"Deferred Signals (Safe Signals)"> | |
1736 | for more details. | |
1737 | ||
1738 | =end original | |
1739 | ||
1740 | Perl 5.8.0 から、インタプリタが I<安全な> 状態になるまでシグナル操作を | |
1741 | 延期することでシグナルはより安全になりました。 | |
1742 | さらなる詳細については L<perl58delta/"Safe Signals"> と | |
1743 | L<perlipc/"Deferred Signals (Safe Signals)"> を参照してください。 | |
1744 | ||
1745 | =begin original | |
1746 | ||
1747 | Safe signals is the default behavior, and the old, immediate, unsafe | |
1748 | signalling behavior is only in effect in the following situations: | |
1749 | ||
1750 | =end original | |
1751 | ||
1752 | 安全なシグナルはデフォルトの振る舞いで、古い、即時で、安全でないシグナルの | |
1753 | 振る舞いは以下の状況でのみ有効です。 | |
1754 | ||
1755 | =over 4 | |
1756 | ||
1757 | =item * Perl has been built with C<PERL_OLD_SIGNALS> (see C<perl -V>). | |
1758 | ||
1759 | (Perl が C<PERL_OLD_SIGNALS> でビルドされている(C<perl -V> 参照)。) | |
1760 | ||
1761 | =item * The environment variable C<PERL_SIGNALS> is set to C<unsafe> (see L<perlrun/"PERL_SIGNALS">). | |
1762 | ||
1763 | (環境変数 C<PERL_SIGNALS> が C<unsafe> に設定されている (L<perlrun/"PERL_SIGNALS"> 参照)。) | |
1764 | ||
1765 | =item * The module L<Perl::Unsafe::Signals> is used. | |
1766 | ||
1767 | (L<Perl::Unsafe::Signals> モジュールが使われている。) | |
1768 | ||
1769 | 256 | =back |
1770 | 257 | |
1771 | = | |
258 | =head1 著者および著作権 | |
1772 | 259 | |
1773 | ||
260 | Arthur Bergman E<lt>arthur at contiller.seE<gt> | |
1774 | the C<-E<gt>kill()> signalling method cannot be used. | |
1775 | 261 | |
1776 | =end original | |
1777 | ||
1778 | 安全でないシグナルが有効の場合、シグナルの扱いはスレッドセーフではなく、 | |
1779 | C<-E<gt>kill()> シグナルメソッドは使えません。 | |
1780 | ||
1781 | =item Returning closures from threads | |
1782 | ||
1783 | (スレッドからクロージャを返す) | |
1784 | ||
1785 | =begin original | |
1786 | ||
1787 | Returning closures from threads should not be relied upon. Depending of the | |
1788 | Perl version and the application code, results may range from success, to | |
1789 | (apparently harmless) warnings of leaked scalar, or all the way up to crashing | |
1790 | of the Perl interpreter. | |
1791 | ||
1792 | =end original | |
1793 | ||
1794 | スレッドからクロージャを返すことを信頼するべきではありmせん。 | |
1795 | Perl バージョンとアプリケーションコードに依存して、成功から(おそらくは | |
1796 | 無害な)リークしたスカラの警告、Perl インタプリタのクラッシュまでさまざまな | |
1797 | 結果となります。 | |
1798 | ||
1799 | =item Returning objects from threads | |
1800 | ||
1801 | (スレッドからオブジェクトを返す) | |
1802 | ||
1803 | =begin original | |
1804 | ||
1805 | Returning objects from threads does not work. Depending on the classes | |
1806 | involved, you may be able to work around this by returning a serialized | |
1807 | version of the object (e.g., using L<Data::Dumper> or L<Storable>), and then | |
1808 | reconstituting it in the joining thread. If you're using Perl 5.10.0 or | |
1809 | later, and if the class supports L<shared objects|threads::shared/"OBJECTS">, | |
1810 | you can pass them via L<shared queues| Thread::Queue>. | |
1811 | ||
1812 | =end original | |
1813 | ||
1814 | スレッドからオブジェクトを返すのは動作しません。 | |
1815 | 使うクラスに依存して、(例えば L<Data::Dumper> や L<Storable> を使って) | |
1816 | 直列化されたオブジェクトを返して、join したスレッドでこれを再構成することで | |
1817 | これを回避できることがあります。 | |
1818 | Perl 5.10.0 以降を使っていて、クラスが | |
1819 | L<共有オブジェクト|threads::shared/"OBJECTS"> に対応しているなら、 | |
1820 | L<共有キュー| Thread::Queue> 経由で渡すことができます。 | |
1821 | ||
1822 | =item END blocks in threads | |
1823 | ||
1824 | (スレッドの END ブロック) | |
1825 | ||
1826 | =begin original | |
1827 | ||
1828 | It is possible to add L<END blocks|perlmod/"BEGIN, UNITCHECK, CHECK, INIT and | |
1829 | END"> to threads by using L<require|perlfunc/"require VERSION"> or | |
1830 | L<eval|perlfunc/"eval EXPR"> with the appropriate code. These C<END> blocks | |
1831 | will then be executed when the thread's interpreter is destroyed (i.e., either | |
1832 | during a C<-E<gt>join()> call, or at program termination). | |
1833 | ||
1834 | =end original | |
1835 | ||
1836 | 適切なコードで L<require|perlfunc/"require VERSION"> や | |
1837 | L<eval|perlfunc/"eval EXPR"> を使うことでスレッドに | |
1838 | L<END ブロック|perlmod/"BEGIN, UNITCHECK, CHECK, INIT and | |
1839 | END"> を追加することは可能です。 | |
1840 | これらの C<END> ブロックはスレッドインタプリタが破壊される | |
1841 | (つまり C<-E<gt>join()> の呼び出しの間かプログラムが終了する)ときに | |
1842 | 実行されます。 | |
1843 | ||
1844 | =begin original | |
1845 | ||
1846 | However, calling any L<threads> methods in such an C<END> block will most | |
1847 | likely I<fail> (e.g., the application may hang, or generate an error) due to | |
1848 | mutexes that are needed to control functionality within the L<threads> module. | |
1849 | ||
1850 | =end original | |
1851 | ||
1852 | しかし、C<END> ブロックのような中で L<threads> メソッドを呼び出すと、 | |
1853 | L<threads> モジュール内部の機能を制御するために必要なミューテックスのために | |
1854 | ほぼ間違いなく I<失敗> (例えばアプリケーションがハングしたりエラーが | |
1855 | 発生したり)します。 | |
1856 | ||
1857 | =begin original | |
1858 | ||
1859 | For this reason, the use of C<END> blocks in threads is B<strongly> | |
1860 | discouraged. | |
1861 | ||
1862 | =end original | |
1863 | ||
1864 | この理由により、スレッド中の C<END> の使用は B<強く> 非推奨です。 | |
1865 | ||
1866 | =item Perl Bugs and the CPAN Version of L<threads> | |
1867 | ||
1868 | (Perl のバグと CPAN 版の L<threads>) | |
1869 | ||
1870 | =begin original | |
1871 | ||
1872 | Support for threads extends beyond the code in this module (i.e., | |
1873 | F<threads.pm> and F<threads.xs>), and into the Perl interpreter itself. Older | |
1874 | versions of Perl contain bugs that may manifest themselves despite using the | |
1875 | latest version of L<threads> from CPAN. There is no workaround for this other | |
1876 | than upgrading to the latest version of Perl. | |
1877 | ||
1878 | =end original | |
1879 | ||
1880 | スレッドの対応は | |
1881 | このモジュール (つまり F<threads.pm> と F<threads.xs>) のコードを超えて | |
1882 | Perl インタプリタ自身に拡張されます。 | |
1883 | より古いバージョンの Perl には CPAN から最新版の L<threads> を使っているにも | |
1884 | 関わらず自分自身を示すというバグがあります。 | |
1885 | Perl を最新版にアップグレードする以外に回避方法はありません。 | |
1886 | ||
1887 | =begin original | |
1888 | ||
1889 | Even with the latest version of Perl, it is known that certain constructs | |
1890 | with threads may result in warning messages concerning leaked scalars or | |
1891 | unreferenced scalars. However, such warnings are harmless, and may safely be | |
1892 | ignored. | |
1893 | ||
1894 | =end original | |
1895 | ||
1896 | 最新版の Perl でも、スレッドとある種の構造はリークしたスカラや | |
1897 | 参照されていないスカラ内観する警告メッセージが出ることがあります。 | |
1898 | しかし、このような警告は無害で、安全に無視できます。 | |
1899 | ||
1900 | =begin original | |
1901 | ||
1902 | You can search for L<threads> related bug reports at | |
1903 | L<http://rt.cpan.org/Public/>. If needed submit any new bugs, problems, | |
1904 | patches, etc. to: L<http://rt.cpan.org/Public/Dist/Display.html?Name=threads> | |
1905 | ||
1906 | =end original | |
1907 | ||
1908 | L<threads> 関連のバグ報告を L<http://rt.cpan.org/Public/> で探せます。 | |
1909 | 新しいバグ、問題、パッチなどを投稿する必要があるなら: | |
1910 | L<http://rt.cpan.org/Public/Dist/Display.html?Name=threads> | |
1911 | ||
1912 | =back | |
1913 | ||
1914 | =head1 REQUIREMENTS | |
1915 | ||
1916 | (必要条件) | |
1917 | ||
1918 | =begin original | |
1919 | ||
1920 | Perl 5.8.0 or later | |
1921 | ||
1922 | =end original | |
1923 | ||
1924 | Perl 5.8.0 以降 | |
1925 | ||
1926 | =head1 SEE ALSO | |
1927 | ||
1928 | =begin original | |
1929 | ||
1930 | L<threads> Discussion Forum on CPAN: | |
1931 | L<http://www.cpanforum.com/dist/threads> | |
1932 | ||
1933 | =end original | |
1934 | ||
1935 | CPAN の L<threads> ディスカッションフォーラム: | |
1936 | L<http://www.cpanforum.com/dist/threads> | |
1937 | ||
1938 | =begin original | |
1939 | ||
1940 | Annotated POD for L<threads>: | |
1941 | L<http://annocpan.org/~JDHEDDEN/threads-1.72/threads.pm> | |
1942 | ||
1943 | =end original | |
1944 | ||
1945 | L<threads> の注釈付き POD: | |
1946 | L<http://annocpan.org/~JDHEDDEN/threads-1.72/threads.pm> | |
1947 | ||
1948 | =begin original | |
1949 | ||
1950 | Source repository: | |
1951 | L<http://code.google.com/p/threads-shared/> | |
1952 | ||
1953 | =end original | |
1954 | ||
1955 | ソースレポジトリ: | |
1956 | L<http://code.google.com/p/threads-shared/> | |
1957 | ||
1958 | L<threads::shared>, L<perlthrtut> | |
1959 | ||
1960 | =begin original | |
1961 | ||
1962 | L<http://www.perl.com/pub/a/2002/06/11/threads.html> and | |
1963 | L<http://www.perl.com/pub/a/2002/09/04/threads.html> | |
1964 | ||
1965 | =end original | |
1966 | ||
1967 | L<http://www.perl.com/pub/a/2002/06/11/threads.html> と | |
1968 | L<http://www.perl.com/pub/a/2002/09/04/threads.html> | |
1969 | ||
1970 | =begin original | |
1971 | ||
1972 | Perl threads mailing list: | |
1973 | L<http://lists.cpan.org/showlist.cgi?name=iThreads> | |
1974 | ||
1975 | =end original | |
1976 | ||
1977 | Perl スレッドメーリングリスト: | |
1978 | L<http://lists.cpan.org/showlist.cgi?name=iThreads> | |
1979 | ||
1980 | =begin original | |
1981 | ||
1982 | Stack size discussion: | |
1983 | L<http://www.perlmonks.org/?node_id=532956> | |
1984 | ||
1985 | =end original | |
1986 | ||
1987 | スタックサイズの議論: | |
1988 | L<http://www.perlmonks.org/?node_id=532956> | |
1989 | ||
1990 | =head1 AUTHOR | |
1991 | ||
1992 | Artur Bergman E<lt>sky AT crucially DOT netE<gt> | |
1993 | ||
1994 | CPAN version produced by Jerry D. Hedden <jdhedden AT cpan DOT org> | |
1995 | ||
1996 | =head1 LICENSE | |
1997 | ||
1998 | 262 | threads is released under the same license as Perl. |
1999 | 263 | |
2000 | ||
264 | Thanks to | |
2001 | 265 | |
2002 | Richard Soderberg E<lt> | |
266 | Richard Soderberg E<lt>rs at crystalflame.netE<gt> | |
2003 | 267 | Helping me out tons, trying to find reasons for races and other weird bugs! |
2004 | 268 | |
2005 | Simon Cozens E<lt>simon | |
269 | Simon Cozens E<lt>simon at brecon.co.ukE<gt> | |
2006 | 270 | Being there to answer zillions of annoying questions |
2007 | 271 | |
2008 | Rocco Caputo E<lt>troc | |
272 | Rocco Caputo E<lt>troc at netrus.netE<gt> | |
2009 | 273 | |
2010 | Vipul Ved Prakash E<lt>mail | |
274 | Vipul Ved Prakash E<lt>mail at vipul.netE<gt> | |
2011 | Helping with debugging | |
275 | Helping with debugging. | |
2012 | 276 | |
2013 | ||
277 | please join perl-ithreads@perl.org for more information | |
2014 | Stack size API | |
2015 | 278 | |
2016 | = | |
279 | =head1 SEE ALSO | |
2017 | 280 | |
2018 | ||
281 | L<threads::shared>, L<perlthrtut>, | |
282 | L<http://www.perl.com/pub/a/2002/06/11/threads.html>, | |
2020 | ||
283 | L<perlcall>, L<perlembed>, L<perlguts> | |
2021 | Update: Kentaro Shirakata <argrath@ub32.org> (5.8.4, 1.67-) | |
2022 | Status: completed | |
2023 | ||
2024 | =end meta |