Thread-Use-0.05 > Thread::Use

名前

Thread::Use - スレッドの中でのみモジュールを使う

概要

    use Thread::Use;

    threads->new( sub {
     useit Module;
     useit Module qw(parameters);

     noit Module;
     noit Module qw(parameters);
    } );

説明

                  *** 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以前、
 あるいはスレッドが有効になっていない場合は、使うことができない。

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

When you are programming threaded applications and are interested in saving memory by selectively loading modules inside threads only, you will find that even if you use a module inside a thread, it is in fact available to all threads, including the main thread.

あなたがスレッドアプリケーションをプログラミングしていて、メモリを節約 しようとスレッドの中でのみモジュールを選択的にロードしようとする。 スレッド内でモジュールをuseしたとしても、実際にはメインスレッドも 含めてスレッドでモジュールが有効になってしまうことに気づくだろう。

This is caused by the fact that use is executed at compile time. At which time Perl doesn't know anything about threads yet.

これはコンパイル時にuseが実行されてしまうという事実による。この時点では、 Perlはスレッドについてまだ何も知らないのだ。

However, some modules, specifically the ones that are (partly) implemented in XS, do not (yet) survive the cloning process that is involved with creating threads. So you can only use these modules inside threads only. But if you use a module, it will be read in at compile time.

ところが、あるモジュール、特に(部分的に)XSで実装されているものは、 スレッドが生成される際に発生するクロン処理を(まだ)生き残れない。 そのため、こういったモジュールはスレッド内でしか利用できない。しかし、 モジュールをuseすれば、それはコンパイル時に読み込まれてしまう。

Of course, a use is nothing more than a require followed by a call to the "import" class routine (if available). But that doesn't feel natural to do. So this module allows you to use the useit (for use in threads command to indicate that a module should only be used inside a thread.

もちろん、useは、requireと、(利用可能なら)それに引き続くクラス ルーチンを"import"する呼び出しと同じものだ。だがそれを使うのが自然とも 思えない。そこで、このモジュールを使うと、useituse in threads の意味)コマンドを使ってスレッド内でのみ利用されるモジュールを指定できる。

For example: suppose you only need the PerlIO::gzip module inside a thread:

例:PerlIO::gzipモジュールをスレッド内部でのみ使いたいとする:

 use Thread::Use;  # プログラム内のどこにでも置ける

 threads->new( \&zipfile,filename,contents ); # スレッドの開始

 sub zipfile {
   useit PerlIO::gzip;  # このスレッドの中でのみ使う
   open( my $out,'>:gzip', $_[0] ) or die "$_[0]: $!";
   print $out $_[1];
   close( $out );
 }

For completeness, it is also possible to pass any parameters as you would with the use command. So:

完全を期して、useコマンドで用いるであろうパラメータを渡すことも 可能だ。そこで:

 sub storable {
   useit Storable qw(freeze); # スレッドの名前空間に"freeze"をエクスポート
   my $frozen = freeze( \@_ );
 }

or to use the opposite no equivalent;

あるいは、反対のnoでも同じように;

 sub warnings {
   useit warnings "all";
   # special code
   noit warnings "all";
 }

必要なモジュール

 (none)

(なし)

警告

This modules is still experimental and subject to change. At the current stage it is more a proof of concept than anything else.

このモジュールはまだ実験的であり、変更の対象である。現段階では、 他のモジュールより概念実証的である。

There is no way to useit a module without having it call the "import" class method, as you can do with use Module (). However, a simple require Module does exactly the same, so if you want to useit a module without calling its "import" method, you should just use require.

"import"クラスメソッドを呼び出さずにモジュールをuseitすることはできない。 use Module ()とするのは可能だ。しかし、単純なrequire Moduleは正確に 同じ事(importしないこと)をする。そのため、モジュールをuseitするのに その"import"メソッドを呼び出したくないなら、単にrequireを使うべきだ。

作者

Elizabeth Mattijsen, <liz@dijkmat.nl>.

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

著作権

Copyright (c) 2002-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.