Thread-Running-0.05 > Thread::Running

名前

Thread::Running - スレッドが実行中かどうかを非ブロックで調べる

概要

    use Thread::Running;      # running(), exited()とtojoin()をエクスポート
    use Thread::Running qw(running);   # running()だけエクスポート
    use Thread::Running ();   # threadsメソッドのみ

    my $thread = threads->new( sub { whatever } );
    while ($thread->running) {
    # do your stuff
    }

    $_->join foreach threads->tojoin;

    until (threads->exited( $tid )) {
    # do your stuff
    }

    sleep 1 while threads->running;

解説

                  *** A note of CAUTION ***

 This module only functions on Perl versions 5.8.0 and later.
 And then only when threads are enabled with -Dusethreads.  It
 is of no use with any version of Perl before 5.8.0 or without
 threads enabled.

                  *************************

                  *** 注   意   書   き ***

 このモジュールは、Perlのバージョン5.8.0以降で、かつ、-Dusethreads
 オプションでスレッドが有効になっている場合にのみ機能する。5.8.0以前、
 あるいはスレッドが有効になっていない場合は、使うことができない。

                  *************************

This module adds three features to threads that are sorely missed by some: you can check whether a thread is running, whether it can be joined or whether it has exited without waiting for that thread to be finished (non-blocking).

このモジュールは、人によっては無いと大層困ってしまう三つの機能を追加する: あるスレッドが実行中かどうか、join可能かどうか、あるいは終了したのかどうかを、 そのスレッドが終了するのを待たずに(非ブロックで)チェックできる。

メソッド

These are the methods.

以下のメソッドがある。

running

 sleep 1 while threads->running; # 全てのスレッドが実行を停止するまで待つ

 sleep 1 while $thread->running; # このスレッドが実行を停止するまで待つ

 @running = threads->running( @thread );  # まだ実行中のスレッドのリスト

 while (running( @tid )) {  # サブルーチン:少なくとも一つはまだ実行中
 # do your stuff
 }

The "running" method allows you to check whether one or more threads are still running. It accepts one or more thread objects or thread ID's (as obtained by the threads::tid() method).

"running"メソッドを使うと、一つ以上のスレッドがまだ実行中であるかどうかを チェックできる。メソッドは一つ以上のスレッドオブジェクト、または (threads::tid()メソッドで取得した)スレッドIDを受け付ける。

If called as a class method or as a subroutine without parameters, then it will check all threads of which it knows. If called as an instance method without parameters, it will only check the thread associated with the object.

クラスメソッド、あるいはパラメータ無しのサブルーチンとして呼ぶと、 把握している全スレッドをチェックする。パラメータ無しでインスタンスメソッド として呼ぶと、そのオブジェクトに結びついているスレッドだけをチェックする。

In list context it returns the thread ID's of the threads that are still running. In scalar context, it just returns 1 or 0 to indicate whether any of the (implicitely) indicated threads is still running.

リストコンテキストでは、まだ実行中のスレッドのスレッドIDを返す。 スカラーコンテキストでは、単に1か0を返すことによって、(暗黙的に)指示された スレッドのいずれかがまだ実行中かどうかを示す。

tojoin

 sleep 1 until threads->tojoin; # いずれかのスレッドがjoinできるまで待つ

 sleep 1 until $thread->tojoin; # このスレッドがjoinできるまで待つ

 warn "Come on and join!\n" if threads->tojoin( $thread );

 $_->join foreach threads->tojoin; # join可能な全スレッドをjoinする

The "tojoin" method allows you to check whether one or more threads have finished executing and can be joined. It accepts one or more thread objects or thread ID's (as obtained by the threads::tid() method).

"tojoin"メソッドを使うと、一つ以上のスレッドが実行を終了していてjoin可能 かどうかをチェックできる。メソッドは一つ以上のスレッドオブジェクト、または (threads::tid()メソッドで取得した)スレッドIDを受け付ける。

If called as a class method or as a subroutine without parameters, then it will check all threads of which it knows. If called as an instance method without parameters, it will only check the thread associated with the object.

クラスメソッド、あるいはパラメータ無しのサブルーチンとして呼ぶと、 把握している全スレッドをチェックする。パラメータ無しでインスタンスメソッド として呼ぶと、そのオブジェクトに結びついているスレッドだけをチェックする。

In list context it returns thread objects of the threads that can be joined. In scalar context, it just returns 1 or 0 to indicate whether any of the (implicitely) indicated threads can be joined.

リストコンテキストでは、join可能なスレッドのスレッドオブジェクトを返す。 スカラーコンテキストでは、単に1か0を返すことによって、(暗黙的に)指示された スレッドのいずれかがjoin可能かどうかを示す。

exited

 sleep 1 until $thread->exited; # このスレッドが終了するまで待つ

 sleep 1 until threads->exited; # 全てのスレッドが終了するまで待つ

 @exited = threads->exited( @thread ); # 終了してしまったスレッド

 until (exited( @tid )) { # サブルーチン:全てのスレッドが終了するまで
 # do your stuff
 }

The "exited" method allows you to check whether all of one or more threads have exited. It accepts one or more thread objects or thread ID's (as obtained by the threads::tid() method).

"exited"メソッドを使うと、一つ以上の全スレッドが終了したかどうかを チェックできる。メソッドは一つ以上のスレッドオブジェクト、または (threads::tid()メソッドで取得した)スレッドIDを受け付ける。

If called as a class method or as a subroutine without parameters, then it will check all threads of which it knows. If called as an instance method without parameters, it will only check the thread associated with the object.

クラスメソッド、あるいはパラメータ無しのサブルーチンとして呼ぶと、 把握している全スレッドをチェックする。パラメータ無しでインスタンスメソッド として呼ぶと、そのオブジェクトに結びついているスレッドだけをチェックする。

In list context it returns the thread ID's of the threads that have exited. In scalar context, it just returns 1 or 0 to indicate whether all of the (implicitely) indicated threads have exited.

リストコンテキストでは、終了したスレッドのスレッドIDを返す。 スカラーコンテキストでは、単に1か0を返すことによって、(暗黙的に)指示された スレッドが全て終了しているかどうかを示す。

必要なモジュール

 load (どのバージョンでも)
 Thread::Exit (0.06)

警告

This module is dependent on the Thread::Exit module, with all of its CAVEATS applicable.

このモジュールはThread::Exitに依存しているので、その警告が全て適用される。

This module uses the load module to make sure that subroutines are loaded only when they are needed.

このモジュールはloadモジュールを使うことで、必要となったときだけ サブルーチンをロードするのを保証している。

TODO

Examples should be added.

例を追加する。

作者

Elizabeth Mattijsen, <liz@dijkmat.nl>.

Please report bugs to <perlbugs@dijkmat.nl>.

著作権

Copyright (c) 2003 Elizabeth Mattijsen <liz@dijkmat.nl>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

参考

threads, Thread::Exit, load.