[pod] [xml]

名前

threads - インタプリタスレッドの使用を可能にするPerl拡張

概要

    use threads;
    sub start_thread {
	print "Thread started\n";
    }
    my $thread  = threads->create("start_thread","argument");
    my $thread2 = $thread->create(sub { print "I am a thread"},"argument");
    my $thread3 = async { foreach (@files) { ... } };
    $thread->join();
    $thread->detach();
    $thread = threads->self();
    $thread = threads->object( $tid );
    $thread->tid();
    threads->tid();
    threads->self->tid();
    threads->yield();
    threads->list();

説明

Perl 5.6 introduced something called interpreter threads. Interpreter threads are different from "5005threads" (the thread model of Perl 5.005) by creating a new perl interpreter per thread and not sharing any data or state between threads by default.

Perl 5.6 はインタープリッタ・スレッドと呼ばれるものを導入した。 インタープリッタ・スレッドは、スレッド毎に新たにPerlインタプリタを生成する ことによって、また、デフォルトではいかなるデータや状態もスレッド間で共有しない ことによって、5005スレッド(Perl 5.005 におけるスレッドモデル)とは区別される。

Prior to perl 5.8 this has only been available to people embedding perl and for emulating fork() on windows.

perl 5.8より前では、これはperlをembedする人々にとってのみ、 そしてwindowsでfork()をエミュレートするためにのみ利用可能であった。

The threads API is loosely based on the old Thread.pm API. It is very important to note that variables are not shared between threads, all variables are per default thread local. To use shared variables one must use threads::shared.

threads APIは、いい加減な形で古いThread.pm APIに基づいている。 変数はスレッド間で共有されず、全ての変数はデフォルトで スレッドローカルなものであることに注意しておくことが非常に重要だ。 共有変数を利用するには、threads::sharedを使わなければならない。

It is also important to note that you must enable threads by doing use threads as early as possible in the script itself and that it is not possible to enable threading inside an eval "", do, require, or use. In particular, if you are intending to share variables with threads::shared, you must use threads before you use threads::shared and threads will emit a warning if you do it the other way around.

また、スクリプト内ではできるだけ早いうちにuse threadsして スレッドを利用可能にしておくべきだし、 eval "", do,require, or useの内部では スレッド操作ができないことに注意すること。 特にthreads::sharedを使って変数を共有しようとするならば、 use threads::sharedの前にuse threadsしなければならない。 逆にしてしまうとthreadsは警告を発する。

警告

TODO

The current implementation of threads has been an attempt to get a correct threading system working that could be built on, and optimized, in newer versions of perl.

現在のスレッド実装は、より新しいPerlのバージョンにおいて、 正しく動作するスレッドシステムとして構築され、最適化されてきた。

Currently the overhead of creating a thread is rather large, also the cost of returning values can be large. These are areas were there most likely will be work done to optimize what data that needs to be cloned.

現在のところ、スレッドの生成にかかるオーバーヘッドはやや大きく、 また、値を返すためのコストも大きくなりかねない。クローンされる 必要のあるデータが何なのかを最適化する余地があるだろう。

バグ

著者および著作権

Arthur Bergman <sky at nanisky.com>

threads is released under the same license as Perl.

Thanks to

Richard Soderberg <perl at crystalflame.net> Helping me out tons, trying to find reasons for races and other weird bugs!

Simon Cozens <simon at brecon.co.uk> Being there to answer zillions of annoying questions

Rocco Caputo <troc at netrus.net>

Vipul Ved Prakash <mail at vipul.net> Helping with debugging.

please join perl-ithreads@perl.org for more information

参考

threads::shared, perlthrtut, http://www.perl.com/pub/a/2002/06/11/threads.html, perlcall, perlembed, perlguts