threads-0.99 > 1.72 との差分

threads 1.72 と 0.99 の差分

11
22=encoding euc-jp
33
4=head1 NAME
4=head1 名前
55
6=begin original
6threads - インタプリタスレッドの使用を可能にするPerl拡張
77
8threads - Perl interpreter-based threads
8=head1 概要
99
10=end original
10 use threads;
1111
12threads - Perl のインタプリタベースのスレッド
13
14=head1 VERSION
15
16=begin original
17
18This 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
3112 sub start_thread {
32 my @args = @_;
13 print "Thread started\n";
33 print('Thread started: ', join(' ', @args), "\n");
3414 }
35 my $thr = threads->create('start_thread', 'argument');
36 $thr->join();
3715
38 threads->create(sub { print("I am a thread\n"); })->join();
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) { ... } };
3919
40 my $thr2 = async { foreach (@files) { ... } };
20 $thread->join();
41 $thr2->join();
21 $thread->detach();
42 if (my $err = $thr2->error()) {
43 warn("Thread error: $err\n");
44 }
4522
46 # Invoke thread in list context (implicit) so it can return a list
23 $thread = threads->self();
47 my ($thr) = threads->create(sub { return (qw/a b c/); });
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();
5225
53 $thr->detach();
26 $thread->tid();
27 threads->tid();
28 threads->self->tid();
5429
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
6530 threads->yield();
66 yield();
6731
68 # Lists of non-detached threads
32 threads->list();
69 my @threads = threads->list();
70 my $thread_count = threads->list();
7133
72 my @running = threads->list(threads::running);
34=head1 説明
73 my @joinable = threads->list(threads::joinable);
7435
75 # Test thread objects
36Perl 5.6 introduced something called interpreter threads. Interpreter
76 if ($thr1 == $thr2) {
37threads are different from "5005threads" (the thread model of Perl
77 ...
385.005) by creating a new perl interpreter per thread and not sharing
78 }
39any data or state between threads by default.
7940
80 # Manage thread stack size
41Perl 5.6 はインタープリッタ・スレッドと呼ばれるものを導入した。
81 $stack_size = threads->get_stack_size();
42インタープリッタ・スレッドは、スレッド毎に新たにPerlインタプリタを生成することによって、
82 $old_size = threads->set_stack_size(32*4096);
43また、デフォルトではいかなるデータや状態もスレッド間で共有しないことによって、
445005スレッド(Perl 5.005 におけるスレッドモデル)とは区別される。
8345
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);
8946
90 # Get thread's context
47Prior to perl 5.8 this has only been available to people embedding
91 my $wantarray = $thr->wantarray();
48perl and for emulating fork() on windows.
9249
93 # Check thread's state
50perl 5.8より前では、これはperlを埋め込む(embedding)人々にとって、
94 if ($thr->is_running()) {
51そしてwindowsでfork()をエミュレートするためにのみ利用可能であった。
95 sleep(1);
96 }
97 if ($thr->is_joinable()) {
98 $thr->join();
99 }
10052
101 # Send a signal to a thread
53The threads API is loosely based on the old Thread.pm API. It is very
102 $thr->kill('SIGUSR1');
54important to note that variables are not shared between threads, all
55variables are per default thread local. To use shared variables one
56must use threads::shared.
10357
104 # Exit a thread
58threads APIは、いい加減な形で古いThread.pm APIに基づいている。
105 threads->exit();
106
107=head1 DESCRIPTION
108
109=begin original
110
111Perl 5.6 introduced something called interpreter threads. Interpreter threads
112are different from I<5005threads> (the thread model of Perl 5.005) by creating
113a new Perl interpreter per thread, and not sharing any data or state between
114threads by default.
115
116=end original
117
118Perl 5.6 はインタプリタスレッドと呼ばれるものを導入しました。
119インタプリタスレッドは、スレッド毎に新たに Perl インタプリタを
120生成することによって、また、デフォルトではいかなるデータや状態も
121スレッド間で共有しないことによって、I<5005スレッド>
122(Perl 5.005 におけるスレッドモデル)とは区別されます。
123
124=begin original
125
126Prior to Perl 5.8, this has only been available to people embedding Perl, and
127for emulating fork() on Windows.
128
129=end original
130
131Perl 5.8 より前では、これは Perl を組み込むする人々にとってのみ、
132そして Windows で fork() をエミュレートするためにのみ利用可能でした。
133
134=begin original
135
136The I<threads> API is loosely based on the old Thread.pm API. It is very
137important to note that variables are not shared between threads, all variables
138are by default thread local. To use shared variables one must also use
139L<threads::shared>:
140
141=end original
142
143I<threads> API は、古い Thread.pm API におおまかに基づいています。
14459変数はスレッド間で共有されず、全ての変数はデフォルトで
145スレッドローカルなものであることに注意しておくことが非常に重要です
60スレッドローカルなものであることに注意しておくことが非常に重要
146共有変数を利用するには、L<threads::shared> を使わなければなりません
61共有変数を利用するには、threads::sharedを使わなければならない
14762
148 use threads;
63It is also important to note that you must enable threads by doing
149 use threads::shared;
64C<use threads> as early as possible in the script itself and that it
65is not possible to enable threading inside an C<eval "">, C<do>,
66C<require>, or C<use>. In particular, if you are intending to share
67variables with threads::shared, you must C<use threads> before you
68C<use threads::shared> and C<threads> will emit a warning if you do
69it the other way around.
15070
151=begin original
71また、スクリプト内ではできるだけ早いうちにC<use threads>して
152
153It is also important to note that you must enable threads by doing C<use
154threads> as early as possible in the script itself, and that it is not
155possible to enable threading inside an C<eval "">, C<do>, C<require>, or
156C<use>. In particular, if you are intending to share variables with
157L<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> して
16372スレッドを利用可能にしておくべきだし、
164C<eval "">, C<do>, C<require>, C<use> の内部では
73C<eval "">, C<do>,C<require>, or C<use>の内部では
165スレッド操作ができないことに注意してください
74スレッド操作ができないことに注意すること
166特に L<threads::shared> を使って変数を共有しようとするならば、
75特にthreads::sharedを使って変数を共有しようとするならば、
167C<use threads::shared> の前に C<use threads> しなければなりません
76C<use threads::shared>の前にC<use threads>しなければならない
168(逆にしてしまうと C<threads> は警告を発します。)
77逆にしてしまうとC<threads>は警告を発す
16978
17079=over
17180
172=item $thr = threads->create(FUNCTION, ARGS)
81=item $thread = threads->create(function, LIST)
17382
174=begin original
83This will create a new thread with the entry point function and give
84it LIST as parameters. It will return the corresponding threads
85object. The new() method is an alias for create().
17586
176This will create a new thread that will begin execution with the specified
87これはエントリーポイントとなる関数を伴って新しいスレッドを生成し、
177entry point function, and give it the I<ARGS> list as parameters. It will
88リストをパラメータとして与える。対応するスレッドオブジェクトを返す。new()はcreate()の言い換えだ。
178return the corresponding threads object, or C<undef> if thread creation failed.
17989
180=end original
90=item $thread->join
18191
182これは指定されたエントリポイント関数の実行を開始し、引数として
92This will wait for the corresponding thread to join. When the thread
183I<ARGS> リストが与えられる新しいスレッドを作ります。
93finishes, join() will return the return values of the entry point
184対応するスレッドオブジェクトか、スレッド作成に失敗した場合は
94function. If the thread has been detached, an error will be thrown.
185C<undef> を返します。
95If the program exits without all other threads having been either
96joined or detached, then a warning will be issued. (A program exits
97either because one of its threads explicitly calls exit(), or in the
98case of the main thread, reaches the end of the main program file.)
18699
187=begin original
100対応するスレッドがjoinするのを待つ。そのスレッドが終了した時、join()はエントリーポイント関数の戻り値を返す。
101もしそのスレッドがdetachされていたなら、エラーが投げられる。いずれかのスレッドがjoinかdetachされずにプログラムが終了すると、
102警告が出る(プログラムは、スレッドが明示的にexit()を呼び出すか、あるいはメインスレッドにおいてプログラムファイルの終端に達した時に終了する)。
188103
189I<FUNCTION> may either be the name of a function, an anonymous subroutine, or
104=item $thread->detach
190a code ref.
191105
192=end original
106Will make the thread unjoinable, and cause any eventual return value
107to be discarded.
193108
194I<FUNCTION> は関数名、無名サブルーチン、コードリファレンスのいずれかです。
109そのレッドをjoin不可能にし、結果戻り値を放棄
195110
196 my $thr = threads->create('func_name', ...);
111=item threads->self
197 # or
198 my $thr = threads->create(sub { ... }, ...);
199 # or
200 my $thr = threads->create(\&func, ...);
201112
202=begin original
113This will return the thread object for the current thread.
203114
204The C<-E<gt>new()> method is an alias for C<-E<gt>create()>.
115現在のスレッドのスレッドオブジェクトを返す。
205116
206=end original
117=item $thread->tid
207118
208C<-E<gt>new()> メソッドは C<-E<gt>create()> のエイリアスです。
119This will return the id of the thread. Thread IDs are integers, with
120the main thread in a program being 0. Currently Perl assigns a unique
121tid to every thread ever created in your program, assigning the first
122thread to be created a tid of 1, and increasing the tid by 1 for each
123new thread that's created.
209124
210=item $thr->join()
125スレッドのidを返す。スレッドIDは整数であり、プログラムの始まりとなるメインスレッドの値は0である。
126現在のPerlは、生成された最初のスレッドのtidに1を与え、新しいスレッドが生成されるたびにtidの値を1増やしていくことによって、プログラム中に生成される全てのスレッドに一意なtidを割り振る。
211127
212=begin original
128NB the class method C<< threads->tid() >> is a quick way to get the
129current thread id if you don't have your thread object handy.
213130
214This will wait for the corresponding thread to complete its execution. When
131注意 あなたがスレッドオブジェクトを利用できない場合、クラスメソッドC<< threads->tid() >>は、現在のスレッドidを手にする近道である。
215the thread finishes, C<-E<gt>join()> will return the return value(s) of the
216entry point function.
217132
218=end original
133=item threads->object( tid )
219134
220対応するスレッドが実行を終了するのを待ちます。
135This will return the thread object for the thread associated with the
221そのスレッドが終了した時、C<-E<gt>join()>
136specified tid. Returns undef if there is no thread associated with the tid
222エントリポイント関数の戻り値を返します。
137or no tid is specified or the specified tid is undef.
223138
224=begin original
139指定されたtidに関連しているスレッドのオブジェクトを返す。もしそのtidと関連しているスレッドがない場合、あるいはtidが指定されていない、指定されたtidがundefの場合、メソッドはundefを返す。
225140
226The context (void, scalar or list) for the return value(s) for C<-E<gt>join()>
141=item threads->yield();
227is determined at the time of thread creation.
228142
229=end original
230
231C<-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
264See L</"THREAD CONTEXT"> for more details.
265
266=end original
267
268さらなる詳細については L</"THREAD CONTEXT"> を参照してください。
269
270=begin original
271
272If the program exits without all threads having either been joined or
273detached, then a warning will be issued.
274
275=end original
276
277全てのスレッドが join されるか detach される前にプログラムが終了した場合、
278警告が発生します。
279
280=begin original
281
282Calling C<-E<gt>join()> or C<-E<gt>detach()> on an already joined thread will
283cause 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
294Makes the thread unjoinable, and causes any eventual return value to be
295discarded. When the program exits, any detached threads that are still
296running are silently terminated.
297
298=end original
299
300スレッドを join 不可能にし、最終的な返り値を捨てるようにします。
301プログラムが終了するとき、まだ実行中の detach されたスレッドは暗黙に
302終了します。
303
304=begin original
305
306If the program exits without all threads having either been joined or
307detached, then a warning will be issued.
308
309=end original
310
311全てのスレッドが join されるか detach される前にプログラムが終了した場合、
312警告が発生します。
313
314=begin original
315
316Calling C<-E<gt>join()> or C<-E<gt>detach()> on an already detached thread
317will 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
328Class 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
338Class 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
348Returns the ID of the thread. Thread IDs are unique integers with the main
349thread 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
362Class 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
372If you add the C<stringify> import option to your C<use threads> declaration,
373then using a threads object in a string or a string context (e.g., as a hash
374key) will cause its ID to be used as the value:
375
376=end original
377
378C<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
391This will return the I<threads> object for the I<active> thread associated
392with the specified thread ID. Returns C<undef> if there is no thread
393associated with the TID, if the thread is joined or detached, if no TID is
394specified or if the specified TID is undef.
395
396=end original
397
398指定されたスレッドに関連するアクティブな I<threads> オブジェクトを返します。
399もし TID で指定されたスレッドがない場合、join や detach されている場合、
400TID が指定されていない場合、指定された TID が undef の
401場合、メソッドは C<undef> を返します。
402
403=item threads->yield()
404
405=begin original
406
407143This is a suggestion to the OS to let this thread yield CPU time to other
408144threads. What actually happens is highly dependent upon the underlying
409145thread implementation.
410146
411=end original
147このスレッドが他のスレッドにCPU時間を譲ってもいいということをOSに示唆する。実際に起こることは、基になっているスレッド実装に大きく依存している。
412148
413このスレッドが他のスレッドに CPU 時間を譲ってもいいということを OS
149You may do C<use threads qw(yield)> then use just a bare C<yield> in your
414示唆します。
415実際に起こることは、基になっているスレッド実装に大きく依存しています。
416
417=begin original
418
419You may do C<use threads qw(yield)>, and then just use C<yield()> in your
420150code.
421151
422=end original
152コード内では、C<use threads qw(yield)>してから、たんに裸のC<yield>を使ってもよい。
423153
424コード内では、C<use threads qw(yield)> してから、単に C<yield()> を
154=item threads->list();
425使えます。
426155
427=item threads->list()
156This will return a list of all non joined, non detached threads.
428157
429=item threads->list(threads::all)
158joinおよびdetachされていない全てのスレッドのリストを返す。
430159
431=item threads->list(threads::running)
432
433=item threads->list(threads::joinable)
434
435=begin original
436
437With no arguments (or using C<threads::all>) and in a list context, returns a
438list of all non-joined, non-detached I<threads> objects. In a scalar context,
439returns a count of the same.
440
441=end original
442
443引数なしで (または C<threads::all> を使って) リストコンテキストの場合、
444join されておらず、detach されていない全ての I<threads> オブジェクトの
445リストを返します。
446スカラコンテキストでは、上述のものの数を返します。
447
448=begin original
449
450With a I<true> argument (using C<threads::running>), returns a list of all
451non-joined, non-detached I<threads> objects that are still running.
452
453=end original
454
455引数が I<真> の (または C<threads::running> を使った) 場合、
456join されておらず、detach されていない、まだ実行中の
457I<threads> オブジェクトのリストを返します。
458
459=begin original
460
461With a I<false> argument (using C<threads::joinable>), returns a list of all
462non-joined, non-detached I<threads> objects that have finished running (i.e.,
463for which C<-E<gt>join()> will not I<block>).
464
465=end original
466
467引数が I<偽> の (または C<threads::joinable> を使った) 場合、
468join されておらず、detach されていない、実行が終了した
469(つまり C<-E<gt>join()> が I<ブロック> されない) I<threads> オブジェクトの
470リストを返します。
471
472=item $thr1->equal($thr2)
473
474=begin original
475
476Tests if two threads objects are the same thread or not. This is overloaded
477to the more natural forms:
478
479=end original
480
4812 つのスレッドオブジェクトが同じスレッドかどうかをテストします。
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
500160=item async BLOCK;
501161
502=begin original
503
504162C<async> creates a thread to execute the block immediately following
505it. This block is treated as an anonymous subroutine, and so must have a
163it. This block is treated as an anonymous sub, and so must have a
506semicolon after the closing brace. Like C<threads-E<gt>create()>, C<async>
164semi-colon after the closing brace. Like C<< threads->new >>, C<async>
507returns a I<threads> object.
165returns a thread object.
508166
509=end original
167C<async>はその直後に続くブロックを実行するスレッドを生成する。このブロックは無名サブルーチンとして扱われるので、閉じブレースの後にセミコロンをつけなければならない。C<< threads->new >>同様、C<async>はスレッドオブジェクトを返す。
510168
511C<async> はその直後に続くブロックを実行するスレッドを生成します。
512このブロックは無名サブルーチンとして扱われるので、閉じ大括弧の後に
513セミコロンをつけなければなりません。
514C<threads-E<gt>create()> 同様、C<async> は
515I<threads> オブジェクトを返します。
516
517=item $thr->error()
518
519=begin original
520
521Threads are executed in an C<eval> context. This method will return C<undef>
522if the thread terminates I<normally>. Otherwise, it returns the value of
523C<$@> 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<$@> の値を
530C<無効> コンテキストで返します。
531
532=item $thr->_handle()
533
534=begin original
535
536This I<private> method returns the memory location of the internal thread
537structure associated with a threads object. For Win32, this is a pointer to
538the C<HANDLE> value returned by C<CreateThread> (i.e., C<HANDLE *>); for other
539platforms, it is a pointer to the C<pthread_t> structure used in the
540C<pthread_create> call (i.e., C<pthread_t *>).
541
542=end original
543
544この I<プライベート> メソッドは、スレッドオブジェクトに関連づけられた
545内部スレッド構造体のメモリ位置を返します。
546Win32 では、これは C<CreateThread> から返される C<HANDLE> 値へのポインタ
547(つまり C<HANDLE *>) です; その他のプラットフォームでは、
548C<pthread_create> 呼び出しで使われる C<pthread_t> 構造体へのポインタ
549(つまり C<pthread_t *>) です。
550
551=begin original
552
553This method is of no use for general Perl threads programming. Its intent is
554to provide other (XS-based) thread modules with the capability to access, and
555possibly manipulate, the underlying thread structure associated with a Perl
556thread.
557
558=end original
559
560このメソッドは、一般的な Perl スレッドプログラミングには無用です。
561このメソッドの目的は、その他の (XS ベースの) スレッドモジュールが、
562Perl スレッドと関連づけられている基礎となるスレッド構造体へのアクセスおよび
563おそらくは操作を可能にすることです。
564
565=item threads->_handle()
566
567=begin original
568
569Class method that allows a thread to obtain its own I<handle>.
570
571=end original
572
573スレッドが自身の I<handle> を得るためのクラスメソッドです。
574
575169=back
576170
577=head1 EXITING A THREAD
171=head1 警告
578172
579(スレッドの終了)
580
581=begin original
582
583The usual method for terminating a thread is to
584L<return()|perlfunc/"return EXPR"> from the entry point function with the
585appropriate 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
598If needed, a thread can be exited at any time by calling
599C<threads-E<gt>exit()>. This will cause the thread to return C<undef> in a
600scalar 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
611When called from the I<main> thread, this behaves the same as C<exit(0)>.
612
613=end original
614
615I<main> スレッドから呼び出されると、C<exit(0)> と同様に振る舞います。
616
617=item threads->exit(status)
618
619=begin original
620
621When called from a thread, this behaves like C<threads-E<gt>exit()> (i.e., the
622exit status code is ignored).
623
624=end original
625
626スレッドから呼び出されると、C<threads-E<gt>exit()> と同様に振る舞います
627(つまり、status 終了コードは無視されます)。
628
629=begin original
630
631When called from the I<main> thread, this behaves the same as C<exit(status)>.
632
633=end original
634
635I<main> スレッドから呼び出されると、C<exit(status)> と同様に振る舞います。
636
637=item die()
638
639=begin original
640
641Calling C<die()> in a thread indicates an abnormal exit for the thread. Any
642C<$SIG{__DIE__}> handler in the thread will be called first, and then the
643thread will exit with a warning message that will contain any arguments passed
644in 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
657Calling L<exit()|perlfunc/"exit EXPR"> inside a thread causes the whole
658application to terminate. Because of this, the use of C<exit()> inside
659threaded code, or in modules that might be used in threaded applications, is
660strongly discouraged.
661
662=end original
663
664スレッドの内部で L<exit()|perlfunc/"exit EXPR"> を呼び出すと、
665アプリケーション全体が終了します。
666これのことにより、スレッドコード内部や、スレッド化された
667アプリケーションで使われるかも知れないモジュールでの C<exit()> の使用は
668強く非推奨です。
669
670=begin original
671
672If 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
685This globally overrides the default behavior of calling C<exit()> inside a
686thread, and effectively causes such calls to behave the same as
687C<threads-E<gt>exit()>. In other words, with this setting, calling C<exit()>
688causes 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
700Because of its global effect, this setting should not be used inside modules
701or the like.
702
703=end original
704
705これはグローバルな効果を持つので、この設定はモジュールのようなものの内部では
706使うべきではありません。
707
708=begin original
709
710The I<main> thread is unaffected by this setting.
711
712=end original
713
714I<main> スレッドはこの設定の影響を受けません。
715
716=item threads->create({'exit' => 'thread_only'}, ...)
717
718=begin original
719
720This overrides the default behavior of C<exit()> inside the newly created
721thread only.
722
723=end original
724
725これは新しく作られたスレッドの内側でだけ C<exit()> のデフォルトの
726振る舞いを上書きします。
727
728=item $thr->set_thread_exit_only(boolean)
729
730=begin original
731
732This can be used to change the I<exit thread only> behavior for a thread after
733it has been created. With a I<true> argument, C<exit()> will cause only the
734thread to exit. With a I<false> argument, C<exit()> will terminate the
735application.
736
737=end original
738
739これは、スレッドの I<スレッドだけ終了> の振る舞いを、スレッドが作られた
740後で変更するために使われます。
741I<真> の値を渡すと、C<exit()> によってスレッドだけが終了します。
742I<偽> の値を渡すと、C<exit()> によってアプリケーションが終了します。
743
744=begin original
745
746The I<main> thread is unaffected by this call.
747
748=end original
749
750I<main> スレッドはこの呼び出しの影響を受けません。
751
752=item threads->set_thread_exit_only(boolean)
753
754=begin original
755
756Class method for use inside a thread to change its own behavior for C<exit()>.
757
758=end original
759
760C<exit()> の振る舞いを変えるためにスレッドの内側で使うための
761クラスメソッドです。
762
763=begin original
764
765The I<main> thread is unaffected by this call.
766
767=end original
768
769I<main> スレッドはこの呼び出しの影響を受けません。
770
771=back
772
773=head1 THREAD STATE
774
775(スレッドの状態)
776
777=begin original
778
779The following boolean methods are useful in determining the I<state> of a
780thread.
781
782=end original
783
784以下の真偽値メソッドはスレッドの I<状態> を決定するのに便利です。
785
786=over
787
788=item $thr->is_running()
789
790=begin original
791
792Returns true if a thread is still running (i.e., if its entry point function
793has not yet finished or exited).
794
795=end original
796
797スレッドがまだ実行されている(つまり、そのエントリポイント関数がまだ完了または
798終了していない)なら真を返します。
799
800=item $thr->is_joinable()
801
802=begin original
803
804Returns true if the thread has finished running, is not detached and has not
805yet been joined. In other words, the thread is ready to be joined, and a call
806to C<$thr-E<gt>join()> will not I<block>.
807
808=end original
809
810スレッドが実行を完了していて、detach も join もされていないなら真を返します。
811言い換えると、このスレッドは join する準備が出来ていて、
812C<$thr-E<gt>join()> の呼び出しは I<ブロック> されません。
813
814=item $thr->is_detached()
815
816=begin original
817
818Returns 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
828Class 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
843As with subroutines, the type of value returned from a thread's entry point
844function may be determined by the thread's I<context>: list, scalar or void.
845The thread's context is determined at thread creation. This is necessary so
846that the context is available to the entry point function via
847L<wantarray()|perlfunc/"wantarray">. The thread may then specify a value of
848the appropriate type to be returned from C<-E<gt>join()>.
849
850=end original
851
852サブルーチンと同様、スレッドのエントリポイント関数から返される値の型は
853スレッドの I<コンテキスト> (リスト、スカラ、無効のいずれか) によって
854決定されます。
855スレッドのコンテキストはスレッド作成時に決定されます。
856これは、コンテキストをエントリポイント関数から
857L<wantarray()|perlfunc/"wantarray"> を使って利用可能にするために必要です。
858それからスレッドは C<-E<gt>join()> から返される適切な型の値を指定します。
859
860=head2 Explicit context
861
862(明示的なコンテキスト)
863
864=begin original
865
866Because thread creation and thread joining may occur in different contexts, it
867may be desirable to state the context explicitly to the thread's entry point
868function. This may be done by calling C<-E<gt>create()> with a hash reference
869as 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
885In the above, the threads object is returned to the parent thread in scalar
886context, 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
888the 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
900Similarly, if you need the threads object, but your thread will not be
901returning 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
914The context type may also be used as the I<key> in the hash reference followed
915by 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
933If not explicitly stated, the thread's context is implied from the context
934of 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
954This returns the thread's context in the same manner as
955L<wantarray()|perlfunc/"wantarray">.
956
957=end original
958
959これは L<wantarray()|perlfunc/"wantarray"> と同じ方法でスレッドの
960コンテキストを返します。
961
962=head2 threads->wantarray()
963
964=begin original
965
966Class method to return the current thread's context. This returns the same
967value as running L<wantarray()|perlfunc/"wantarray"> inside the current
968thread's entry point function.
969
970=end original
971
972現在のスレッドのコンテキストを返すクラスメソッドです。
973現在のスレッドのエントリポイント関数の内側で
974L<wantarray()|perlfunc/"wantarray"> を実行するのと同じ値を返します。
975
976=head1 THREAD STACK SIZE
977
978(スレッドのスタックサイズ)
979
980=begin original
981
982The default per-thread stack size for different platforms varies
983significantly, and is almost always far more than is needed for most
984applications. On Win32, Perl's makefile explicitly sets the default stack to
98516 MB; on most other platforms, the system default is used, which again may be
986much larger than is needed.
987
988=end original
989
990デフォルトのスレッド毎のスタックサイズはプラットフォームによって大きく異なり、
991ほとんど常にほとんどのアプリケーションが必要な量よりはるかに多いです。
992Win32 では、Perl の makefile は明示的にデフォルトのスタックを 16 MB に
993指定しています; その他のほとんどのシステムでは、システムのデフォルトが
994使われますが、やはり必要な量よりはるかに多いです。
995
996=begin original
997
998By tuning the stack size to more accurately reflect your application's needs,
999you may significantly reduce your application's memory usage, and increase the
1000number of simultaneously running threads.
1001
1002=end original
1003
1004スタックサイズをアプリケーションのニーズにより正確に反映させることにより、
1005アプリケーションのメモリ使用量を著しく減少させ、同時実行スレッド数を
1006増やすことができるかもしれません。
1007
1008=begin original
1009
1010Note that on Windows, address space allocation granularity is 64 KB,
1011therefore, setting the stack smaller than that on Win32 Perl will not save any
1012more 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
1026Returns the current default per-thread stack size. The default is zero, which
1027means 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
1039Returns the stack size for a particular thread. A return value of zero
1040indicates 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
1052Sets a new default per-thread stack size, and returns the previous setting.
1053
1054=end original
1055
1056新しいデフォルトのスレッド毎のスタックサイズを設定し、以前の設定を
1057返します。
1058
1059=begin original
1060
1061Some platforms have a minimum thread stack size. Trying to set the stack size
1062below this value will result in a warning, and the minimum stack size will be
1063used.
1064
1065=end original
1066
1067最小スレッドスタックサイズがあるプラットフォームもあります。
1068その値よりスタックサイズを小さくしようとすると警告が出て、最小
1069スタックサイズが使われます。
1070
1071=begin original
1072
1073Some Linux platforms have a maximum stack size. Setting too large of a stack
1074size will cause thread creation to fail.
1075
1076=end original
1077
1078最大スタックサイズのある Linux プラットフォームもあります。
1079大きすぎるスタックサイズを設定するとスレッド作成に失敗します。
1080
1081=begin original
1082
1083If needed, C<$new_size> will be rounded up to the next multiple of the memory
1084page size (usually 4096 or 8192).
1085
1086=end original
1087
1088必要なら、C<$new_size> は次のメモリページサイズ(普通は4096 か 8192)倍数に
1089切り上げられます。
1090
1091=begin original
1092
1093Threads created after the stack size is set will then either call
1094C<pthread_attr_setstacksize()> I<(for pthreads platforms)>, or supply the
1095stack size to C<CreateThread()> I<(for Win32 Perl)>.
1096
1097=end original
1098
1099スタックサイズが設定された後に作られたスレッドは
1100C<pthread_attr_setstacksize()> を呼び出す I<(pthreads プラットフォームの
1101場合)> か C<CreateThread()> にスタックサイズを渡します
1102I<(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
1116This 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
1127The default per-thread stack size may be set at the start of the application
1128through the use of the environment variable C<PERL5_ITHREADS_STACK_SIZE>:
1129
1130=end original
1131
1132デフォルトのスレッド毎のスタックサイズは環境変数
1133C<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
1141This value overrides any C<stack_size> parameter given to C<use threads>. Its
1142primary purpose is to permit setting the per-thread stack size for legacy
1143threaded 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
1155To specify a particular stack size for any individual thread, call
1156C<-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
1169This creates a new thread (C<$thr2>) that inherits the stack size from an
1170existing 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
1189When safe signals is in effect (the default behavior - see L</"Unsafe signals">
1190for more details), then signals may be sent and acted upon by individual
1191threads.
1192
1193=end original
1194
1195安全なシグナルが有効なとき (デフォルトの振る舞いです - さらなる
1196詳細については L</"Unsafe signals"> を参照してください)、シグナルは
1197それぞれのスレッドに対して送られて動作します。
1198
1199173=over 4
1200174
1201=item $thr->kill('SIG...');
175=item A thread exited while %d other threads were still running
1202176
1203=begin original
177A thread (not necessarily the main thread) exited while there were
178still other threads running. Usually it's a good idea to first collect
179the return values of the created threads by joining them, and only then
180exit from the main thread.
1204181
1205Sends the specified signal to the thread. Signal names and (positive) signal
182あるスレッド(メインスレッドである必要はない)が、まだ他のスレッドが実行中にexitした。通常、はじめに生成されたスレッドの戻り値をjoinでもって回収し、それからメインスレッドからexitするのがよい方法だ。
1206numbers are the same as those supported by
1207L<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()>.
1209183
1210=end original
1211
1212指定されたシグナルをスレッドに送ります。
1213シグナル名と(正の)シグナル番号は L<kill()|perlfunc/"kill SIGNAL, LIST"> で
1214対応しているものと同じです。
1215例えば、'SIGTERM', 'TERM' と (OS に依存しますが) 15 は全て
1216C<-E<gt>kill()> への妥当な引数です。
1217
1218=begin original
1219
1220Returns the thread object to allow for method chaining:
1221
1222=end original
1223
1224メソッドチェーンができるように、スレッドオブジェクトを返します:
1225
1226 $thr->kill('SIG...')->join();
1227
1228184=back
1229185
1230=begin original
186=head1 TODO
1231187
1232Signal handlers need to be set up in the threads for the signals they are
188The current implementation of threads has been an attempt to get
1233expected to act upon. Here's an example for I<cancelling> a thread:
189a correct threading system working that could be built on,
190and optimized, in newer versions of perl.
1234191
1235=end original
192現在のスレッド実装は、より新しいPerlのバージョンにおいて、
193正しく動作するスレッドシステムとして構築され、最適化されてきた。
1236194
1237シグナルハンドラは対応することを想定しているシグナルに対してスレッドで
195Currently the overhead of creating a thread is rather large,
1238設定される必要があります。
196also the cost of returning values can be large. These are areas
1239以下はスレッドを I<キャンセルする> 例です:
197were there most likely will be work done to optimize what data
198that needs to be cloned.
1240199
1241 use threads;
200現在のところ、スレッドの生成にかかるオーバーヘッドはやや大きく、
201また、値を返すためのコストも大きくなりかねない。クローンされる
202必要のあるデータが何なのかを最適化する余地があるだろう。
1242203
1243 sub thr_func
204=head1 バグ
1244 {
1245 # Thread 'cancellation' signal handler
1246 $SIG{'KILL'} = sub { threads->exit(); };
1247205
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
1262Here's another simplistic example that illustrates the use of thread
1263signalling in conjunction with a semaphore to provide rudimentary I<suspend>
1264and 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
1303CAVEAT: The thread signalling capability provided by this module does not
1304actually send signals via the OS. It I<emulates> signals at the Perl-level
1305such that signal handlers are called in the appropriate thread. For example,
1306sending C<$thr-E<gt>kill('STOP')> does not actually suspend a thread (or the
1307whole process), but does cause a C<$SIG{'STOP'}> handler to be called in that
1308thread (as illustrated above).
1309
1310=end original
1311
1312警告: このモジュールによって提供されているスレッドへのシグナル機能は
1313実際には OS 経由でシグナルを送っていません。
1314シグナルハンドラが適切なスレッドで呼び出されるように Perl レベルでシグナルを
1315I<エミュレート> しています。
1316例えば、C<$thr-E<gt>kill('STOP')> は実際にはスレッド(またはプロセス全体)を
1317停止させませんが、(上述したように) 対象のスレッドの C<$SIG{'STOP'}>
1318ハンドラが呼び出されます。
1319
1320=begin original
1321
1322As such, signals that would normally not be appropriate to use in the
1323C<kill()> command (e.g., C<kill('KILL', $$)>) are okay to use with the
1324C<-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
1334Correspondingly, sending a signal to a thread does not disrupt the operation
1335the thread is currently working on: The signal will be acted upon after the
1336current operation has completed. For instance, if the thread is I<stuck> on
1337an I/O call, sending it a signal will not cause the I/O call to be interrupted
1338such 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
1349Sending 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
1365If the program exits without all threads having either been joined or
1366detached, then this warning will be issued.
1367
1368=end original
1369
1370全てのスレッドが join されるか detach される前にプログラムが終了した場合、
1371この警告が発生します。
1372
1373=begin original
1374
1375NOTE: If the I<main> thread exits, then this warning cannot be suppressed
1376using C<no warnings 'threads';> as suggested below.
1377
1378=end original
1379
1380注意: I<main> スレッドが存在しているなら、後に示唆しているようにこの警告は
1381C<no warnings 'threads';> を使って抑制できません。
1382
1383=item Thread creation failed: pthread_create returned #
1384
1385=begin original
1386
1387See the appropriate I<man> page for C<pthread_create> to determine the actual
1388cause 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
1399A thread terminated in some manner other than just returning from its entry
1400point function, or by using C<threads-E<gt>exit()>. For example, the thread
1401may 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
1413Some platforms have a minimum thread stack size. Trying to set the stack size
1414below this value will result in the above warning, and the stack size will be
1415set 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
1427The specified I<SIZE> exceeds the system's maximum stack size. Use a smaller
1428value for the stack size.
1429
1430=end original
1431
1432指定された I<SIZE> がシステムの最大スタックサイズを超えています。
1433スタックサイズとしてより小さい値を使ってください。
1434
1435=back
1436
1437=begin original
1438
1439If needed, thread warnings can be suppressed by using:
1440
1441=end original
1442
1443もし必要なら、スレッドの警告は以下のものを:
1444
1445 no warnings 'threads';
1446
1447=begin original
1448
1449in 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
1465The particular copy of Perl that you're trying to use was not built using the
1466C<useithreads> configuration option.
1467
1468=end original
1469
1470使おうとしている Perl が C<useithreads> 設定オプションを使って
1471ビルドされていません。
1472
1473=begin original
1474
1475Having threads support requires all of Perl and all of the XS modules in the
1476Perl installation to be rebuilt; it is not just a question of adding the
1477L<threads> module (i.e., threaded and non-threaded Perls are binary
1478incompatible.)
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
1491The stack size of currently extant threads cannot be changed, therefore, the
1492following 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
1505Safe signals must be in effect to use the C<-E<gt>kill()> signalling method.
1506See L</"Unsafe signals"> for more details.
1507
1508=end original
1509
1510C<-E<gt>kill()> シグナルメソッドを使うには安全なシグナルが
1511有効でなければなりません。
1512さらなる詳細については L</"Unsafe signals"> を参照してください。
1513
1514=item Unrecognized signal name: ...
1515
1516=begin original
1517
1518The particular copy of Perl that you're trying to use does not support the
1519specified 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
1534Before you consider posting a bug report, please consult, and possibly post a
1535message to the discussion forum to see if what you've encountered is a known
1536problem.
1537
1538=end original
1539
1540バグ報告を投稿することを考える前に、まず相談してください; そしてできれば
1541遭遇したものが既知の問題かどうかを見るために議論フォーラムにメッセージを
1542投稿してください。
1543
1544206=over
1545207
1546=item Thread-safe modules
208=item - スレッド.
1547209
1548(スレッドセーフなモジュール)
210On some platforms it might not be possible to destroy "parent"
211threads while there are still existing child "threads".
1549212
1550=begin original
213プラットフォームによっては、子スレッドがまだ存在している間は
214親スレッドを破壊することができないことがある。
1551215
1552See L<perlmod/"Making your module threadsafe"> when creating modules that may
216This will possibly be fixed in later versions of perl.
1553be used in threaded applications, especially if those modules use non-Perl
1554data, or XS code.
1555217
1556=end original
218たぶん今後のperlのバージョンではフィックスされるだろう。
1557219
1558スレッド対応アプリケーションで使われるかもしれないモジュールを作るとき、
220=item tidはI32
1559とくにモジュールが非 Perl データや XS コードを使っているときは、
1560L<perlmod/"Making your module threadsafe"> を参照してください。
1561221
1562=item Using non-thread-safe modules
222The thread id is a 32 bit integer, it can potentially overflow.
223This might be fixed in a later version of perl.
1563224
1564(非スレッドーフなモジュルを使う)
225スレッドidは32bit整数である。オバーする可能性がある。
226今後のperlのバージョンではフィックスされるだろう。
1565227
1566=begin original
228=item オブジェクトの戻り
1567229
1568Unfortunately, you may encounter Perl modules that are not I<thread-safe>.
230When you return an object the entire stash that the object is blessed
1569For example, they may crash the Perl interpreter during execution, or may dump
231as well. This will lead to a large memory usage. The ideal situation
1570core on termination. Depending on the module and the requirements of your
232would be to detect the original stash if it existed.
1571application, it may be possible to work around such difficulties.
1572233
1573=end original
234オブジェクトを返すとき、そのオブジェクトがblessされているstashも
235同様である。これはメモリを大量に使用するかもしれない。
236理想的な状態は、存在しているならオリジナルのstashを見つけることなのだが。
1574237
1575残念ながら、I<スレッドセーフ> ではない Perl モジュールに
238=item BEGINブロック内でのスレッドの生成
1576遭遇するかもしれません。
1577例えば、実行中に Perl インタプリタがクラッシュしたり、コアダンプして
1578終了したりするかもしれません。
1579モジュールとアプリケーションの必要事項に依存して、このような問題を
1580回避できることがあります。
1581239
1582=begin original
240Creating threads inside BEGIN blocks (or during the compilation phase
241in general) does not work. (In Windows, trying to use fork() inside
242BEGIN blocks is an equally losing proposition, since it has been
243implemented in very much the same way as threads.)
1583244
1584If the module will only be used inside a thread, you can try loading the
245BEGINブロック内で(つまり一般的にコンパイルフェーズの段階で)
1585module from inside the thread entry point function using C<require> (and
246スレッドを生成することはできない(WindowsのBEGINブロック内でfork()を利用しようとする試みも同様に見込みのない提案である。なぜならスレッドと全く同じ方法で実装されてきたからだ)。
1586C<import> if needed):
1587247
1588=end original
248=item PERL_OLD_SIGNALSはスレッドセーフではない。これからも。
1589249
1590モジュールがスレッドの中でだけ使われているなら、スレッドエントリ関数の
250If your Perl has been built with PERL_OLD_SIGNALS (one has
1591内側から C<require> (および必要なら C<import>) を使ってモジュールを
251to explicitly add that symbol to ccflags, see C<perl -V>),
1592読み込んでみてください:
252signal handling is not threadsafe.
1593253
1594 sub thr_func
254もしあなたがPERL_OLD_SIGNALSオプションを有効にしてPerlをbuiltしてるなら(このシンボルをccflagに明示的に追加しなければならない。C<perl -V>を見よ)、シグナルハンドリングはスレッドセーフではない。
1595 {
1596 require Unsafe::Module
1597 # Unsafe::Module->import(...);
1598255
1599 ....
1600 }
1601
1602=begin original
1603
1604If the module is needed inside the I<main> thread, try modifying your
1605application so that the module is loaded (again using C<require> and
1606C<-E<gt>import()>) after any threads are started, and in such a way that no
1607other 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
1618If the above does not work, or is not adequate for your application, then file
1619a 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
1633On all platforms except MSWin32, the setting for the current working directory
1634is shared among all threads such that changing it in one thread (e.g., using
1635C<chdir()>) will affect all the threads in the application.
1636
1637=end original
1638
1639MSWin32 以外の全てのプラットフォームでは、カレントワーキングディレクトリの
1640設定は全てのスレッドで共有されるので、あるスレッドでこれを変更する
1641(つまり C<chdir()> を使う) とアプリケーションの全てのスレッドに
1642影響します。
1643
1644=begin original
1645
1646On MSWin32, each thread maintains its own the current working directory
1647setting.
1648
1649=end original
1650
1651MSWin32 では、それぞれのカレントワーキングディレクトリ設定を独自に
1652管理しています。
1653
1654=item Environment variables
1655
1656(環境変数)
1657
1658=begin original
1659
1660Currently, on all platforms except MSWin32, all I<system> calls (e.g., using
1661C<system()> or back-ticks) made from threads use the environment variable
1662settings from the I<main> thread. In other words, changes made to C<%ENV> in
1663a thread will not be visible in I<system> calls made by that thread.
1664
1665=end original
1666
1667現在のところ、MSWin32 以外の全てのプラットフォームでは、
1668スレッドによって作られた (C<system()> または逆クォートによる) 全ての
1669I<system> 呼び出しは I<main> スレッドの環境変数設定を使います。
1670言い換えると、スレッドで行った C<%ENV> への変更は、そのスレッドで作られた
1671I<system> 呼び出しでは見えません。
1672
1673=begin original
1674
1675To work around this, set environment variables as part of the I<system> call.
1676For 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
1688On MSWin32, each thread maintains its own set of environment variables.
1689
1690=end original
1691
1692MSWin32 では、各スレッドでは独自の環境変数集合を管理します。
1693
1694=item Parent-child threads
1695
1696(親-子スレッド)
1697
1698=begin original
1699
1700On some platforms, it might not be possible to destroy I<parent> threads while
1701there are still existing I<child> threads.
1702
1703=end original
1704
1705プラットフォームによっては、I<子> スレッドがまだ存在している間は
1706I<親> スレッドを破壊することができないことがあります。
1707
1708=item Creating threads inside special blocks
1709
1710(特殊ブロックの中でスレッドを作る)
1711
1712=begin original
1713
1714Creating threads inside C<BEGIN>, C<CHECK> or C<INIT> blocks should not be
1715relied upon. Depending on the Perl version and the application code, results
1716may range from success, to (apparently harmless) warnings of leaked scalar, or
1717all the way up to crashing of the Perl interpreter.
1718
1719=end original
1720
1721C<BEGIN>, C<CHECK>, C<INIT> ブロックの内側でスレッドを作成することを
1722信頼するべきではありません。
1723Perl バージョンとアプリケーションコードに依存して、成功から(おそらくは
1724無害な)リークしたスカラの警告、Perl インタプリタのクラッシュまでさまざまな
1725結果となります。
1726
1727=item Unsafe signals
1728
1729(安全でないシグナル)
1730
1731=begin original
1732
1733Since Perl 5.8.0, signals have been made safer in Perl by postponing their
1734handling until the interpreter is in a I<safe> state. See
1735L<perl58delta/"Safe Signals"> and L<perlipc/"Deferred Signals (Safe Signals)">
1736for more details.
1737
1738=end original
1739
1740Perl 5.8.0 から、インタプリタが I<安全な> 状態になるまでシグナル操作を
1741延期することでシグナルはより安全になりました。
1742さらなる詳細については L<perl58delta/"Safe Signals"> と
1743L<perlipc/"Deferred Signals (Safe Signals)"> を参照してください。
1744
1745=begin original
1746
1747Safe signals is the default behavior, and the old, immediate, unsafe
1748signalling 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
1769256=back
1770257
1771=begin original
258=head1 著者および著作権
1772259
1773If unsafe signals is in effect, then signal handling is not thread-safe, and
260Arthur Bergman E<lt>arthur at contiller.seE<gt>
1774the C<-E<gt>kill()> signalling method cannot be used.
1775261
1776=end original
1777
1778安全でないシグナルが有効の場合、シグナルの扱いはスレッドセーフではなく、
1779C<-E<gt>kill()> シグナルメソッドは使えません。
1780
1781=item Returning closures from threads
1782
1783(スレッドからクロージャを返す)
1784
1785=begin original
1786
1787Returning closures from threads should not be relied upon. Depending of the
1788Perl 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
1790of the Perl interpreter.
1791
1792=end original
1793
1794スレッドからクロージャを返すことを信頼するべきではありmせん。
1795Perl バージョンとアプリケーションコードに依存して、成功から(おそらくは
1796無害な)リークしたスカラの警告、Perl インタプリタのクラッシュまでさまざまな
1797結果となります。
1798
1799=item Returning objects from threads
1800
1801(スレッドからオブジェクトを返す)
1802
1803=begin original
1804
1805Returning objects from threads does not work. Depending on the classes
1806involved, you may be able to work around this by returning a serialized
1807version of the object (e.g., using L<Data::Dumper> or L<Storable>), and then
1808reconstituting it in the joining thread. If you're using Perl 5.10.0 or
1809later, and if the class supports L<shared objects|threads::shared/"OBJECTS">,
1810you can pass them via L<shared queues| Thread::Queue>.
1811
1812=end original
1813
1814スレッドからオブジェクトを返すのは動作しません。
1815使うクラスに依存して、(例えば L<Data::Dumper> や L<Storable> を使って)
1816直列化されたオブジェクトを返して、join したスレッドでこれを再構成することで
1817これを回避できることがあります。
1818Perl 5.10.0 以降を使っていて、クラスが
1819L<共有オブジェクト|threads::shared/"OBJECTS"> に対応しているなら、
1820L<共有キュー| Thread::Queue> 経由で渡すことができます。
1821
1822=item END blocks in threads
1823
1824(スレッドの END ブロック)
1825
1826=begin original
1827
1828It is possible to add L<END blocks|perlmod/"BEGIN, UNITCHECK, CHECK, INIT and
1829END"> to threads by using L<require|perlfunc/"require VERSION"> or
1830L<eval|perlfunc/"eval EXPR"> with the appropriate code. These C<END> blocks
1831will then be executed when the thread's interpreter is destroyed (i.e., either
1832during a C<-E<gt>join()> call, or at program termination).
1833
1834=end original
1835
1836適切なコードで L<require|perlfunc/"require VERSION"> や
1837L<eval|perlfunc/"eval EXPR"> を使うことでスレッドに
1838L<END ブロック|perlmod/"BEGIN, UNITCHECK, CHECK, INIT and
1839END"> を追加することは可能です。
1840これらの C<END> ブロックはスレッドインタプリタが破壊される
1841(つまり C<-E<gt>join()> の呼び出しの間かプログラムが終了する)ときに
1842実行されます。
1843
1844=begin original
1845
1846However, calling any L<threads> methods in such an C<END> block will most
1847likely I<fail> (e.g., the application may hang, or generate an error) due to
1848mutexes that are needed to control functionality within the L<threads> module.
1849
1850=end original
1851
1852しかし、C<END> ブロックのような中で L<threads> メソッドを呼び出すと、
1853L<threads> モジュール内部の機能を制御するために必要なミューテックスのために
1854ほぼ間違いなく I<失敗> (例えばアプリケーションがハングしたりエラーが
1855発生したり)します。
1856
1857=begin original
1858
1859For this reason, the use of C<END> blocks in threads is B<strongly>
1860discouraged.
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
1872Support for threads extends beyond the code in this module (i.e.,
1873F<threads.pm> and F<threads.xs>), and into the Perl interpreter itself. Older
1874versions of Perl contain bugs that may manifest themselves despite using the
1875latest version of L<threads> from CPAN. There is no workaround for this other
1876than upgrading to the latest version of Perl.
1877
1878=end original
1879
1880スレッドの対応は
1881このモジュール (つまり F<threads.pm> と F<threads.xs>) のコードを超えて
1882Perl インタプリタ自身に拡張されます。
1883より古いバージョンの Perl には CPAN から最新版の L<threads> を使っているにも
1884関わらず自分自身を示すというバグがあります。
1885Perl を最新版にアップグレードする以外に回避方法はありません。
1886
1887=begin original
1888
1889Even with the latest version of Perl, it is known that certain constructs
1890with threads may result in warning messages concerning leaked scalars or
1891unreferenced scalars. However, such warnings are harmless, and may safely be
1892ignored.
1893
1894=end original
1895
1896最新版の Perl でも、スレッドとある種の構造はリークしたスカラや
1897参照されていないスカラ内観する警告メッセージが出ることがあります。
1898しかし、このような警告は無害で、安全に無視できます。
1899
1900=begin original
1901
1902You can search for L<threads> related bug reports at
1903L<http://rt.cpan.org/Public/>. If needed submit any new bugs, problems,
1904patches, etc. to: L<http://rt.cpan.org/Public/Dist/Display.html?Name=threads>
1905
1906=end original
1907
1908L<threads> 関連のバグ報告を L<http://rt.cpan.org/Public/> で探せます。
1909新しいバグ、問題、パッチなどを投稿する必要があるなら:
1910L<http://rt.cpan.org/Public/Dist/Display.html?Name=threads>
1911
1912=back
1913
1914=head1 REQUIREMENTS
1915
1916(必要条件)
1917
1918=begin original
1919
1920Perl 5.8.0 or later
1921
1922=end original
1923
1924Perl 5.8.0 以降
1925
1926=head1 SEE ALSO
1927
1928=begin original
1929
1930L<threads> Discussion Forum on CPAN:
1931L<http://www.cpanforum.com/dist/threads>
1932
1933=end original
1934
1935CPAN の L<threads> ディスカッションフォーラム:
1936L<http://www.cpanforum.com/dist/threads>
1937
1938=begin original
1939
1940Annotated POD for L<threads>:
1941L<http://annocpan.org/~JDHEDDEN/threads-1.72/threads.pm>
1942
1943=end original
1944
1945L<threads> の注釈付き POD:
1946L<http://annocpan.org/~JDHEDDEN/threads-1.72/threads.pm>
1947
1948=begin original
1949
1950Source repository:
1951L<http://code.google.com/p/threads-shared/>
1952
1953=end original
1954
1955ソースレポジトリ:
1956L<http://code.google.com/p/threads-shared/>
1957
1958L<threads::shared>, L<perlthrtut>
1959
1960=begin original
1961
1962L<http://www.perl.com/pub/a/2002/06/11/threads.html> and
1963L<http://www.perl.com/pub/a/2002/09/04/threads.html>
1964
1965=end original
1966
1967L<http://www.perl.com/pub/a/2002/06/11/threads.html> と
1968L<http://www.perl.com/pub/a/2002/09/04/threads.html>
1969
1970=begin original
1971
1972Perl threads mailing list:
1973L<http://lists.cpan.org/showlist.cgi?name=iThreads>
1974
1975=end original
1976
1977Perl スレッドメーリングリスト:
1978L<http://lists.cpan.org/showlist.cgi?name=iThreads>
1979
1980=begin original
1981
1982Stack size discussion:
1983L<http://www.perlmonks.org/?node_id=532956>
1984
1985=end original
1986
1987スタックサイズの議論:
1988L<http://www.perlmonks.org/?node_id=532956>
1989
1990=head1 AUTHOR
1991
1992Artur Bergman E<lt>sky AT crucially DOT netE<gt>
1993
1994CPAN version produced by Jerry D. Hedden <jdhedden AT cpan DOT org>
1995
1996=head1 LICENSE
1997
1998262threads is released under the same license as Perl.
1999263
2000=head1 ACKNOWLEDGEMENTS
264Thanks to
2001265
2002Richard Soderberg E<lt>perl AT crystalflame DOT netE<gt> -
266Richard Soderberg E<lt>rs at crystalflame.netE<gt>
2003267Helping me out tons, trying to find reasons for races and other weird bugs!
2004268
2005Simon Cozens E<lt>simon AT brecon DOT co DOT ukE<gt> -
269Simon Cozens E<lt>simon at brecon.co.ukE<gt>
2006270Being there to answer zillions of annoying questions
2007271
2008Rocco Caputo E<lt>troc AT netrus DOT netE<gt>
272Rocco Caputo E<lt>troc at netrus.netE<gt>
2009273
2010Vipul Ved Prakash E<lt>mail AT vipul DOT netE<gt> -
274Vipul Ved Prakash E<lt>mail at vipul.netE<gt>
2011Helping with debugging
275Helping with debugging.
2012276
2013Dean Arnold E<lt>darnold AT presicient DOT comE<gt> -
277please join perl-ithreads@perl.org for more information
2014Stack size API
2015278
2016=cut
279=head1 SEE ALSO
2017280
2018=begin meta
281L<threads::shared>, L<perlthrtut>,
282L<http://www.perl.com/pub/a/2002/06/11/threads.html>,
2020Translate: まかまか <makamaka@donzoko.net>
283L<perlcall>, L<perlembed>, L<perlguts>
2021Update: Kentaro Shirakata <argrath@ub32.org> (5.8.4, 1.67-)
2022Status: completed
2023
2024=end meta