Lang/Perl/thread
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
#topicpath
/////////////////////////////////////////////////////////...
* 目次 [#p691c381]
#contents();
/////////////////////////////////////////////////////////...
* Thread [#w8ff014e]
//=======================================================...
** thread の生成 [#vb28c0fa]
- threads を使う。
use threads;
# thread の生成
# スレッド関数を new の第1引数に、スレッド関数に渡す引数...
my ($thread_1) = threads->new(\&thread_func, "t1");
# thread 終了の待ち合わせ
$thread_1->join;
# thread 関数
sub thread_func {
# 処理
}
//=======================================================...
** queue による通信 [#lee56779]
- Thread::Queue を使う。
use threads;
use Thread::Queue;
my ($queue_a) = new Thread::Queue;
# thread 生成時に、 thread 関数の引数として Queue を渡す:
my ($thread_a) = threads->new(\&thread_func_a, $queue_a);
$thread_a->join;
- Queue に積む
$queue_a->enqueue(<データ>);
- Queue から引き抜いて $data に格納する
$data = $queue_a->dequeue();
//=======================================================...
* thread 間で共有する変数 [#e3cfa8b5]
- thread 間で共有したい変数は、その旨明示的に宣言する必要...
use threads;
use threads::shared; # thread 間で共有するデータを扱う
my <thread間共有変数> : shared = <初期化値>;
- 例
use threads;
use threads::shared; # thread 間で共有するデータを扱う
my $data : shared = 2; # 変数を初期化する値は、 ": sh...
my @datas : shared = ();
終了行:
#topicpath
/////////////////////////////////////////////////////////...
* 目次 [#p691c381]
#contents();
/////////////////////////////////////////////////////////...
* Thread [#w8ff014e]
//=======================================================...
** thread の生成 [#vb28c0fa]
- threads を使う。
use threads;
# thread の生成
# スレッド関数を new の第1引数に、スレッド関数に渡す引数...
my ($thread_1) = threads->new(\&thread_func, "t1");
# thread 終了の待ち合わせ
$thread_1->join;
# thread 関数
sub thread_func {
# 処理
}
//=======================================================...
** queue による通信 [#lee56779]
- Thread::Queue を使う。
use threads;
use Thread::Queue;
my ($queue_a) = new Thread::Queue;
# thread 生成時に、 thread 関数の引数として Queue を渡す:
my ($thread_a) = threads->new(\&thread_func_a, $queue_a);
$thread_a->join;
- Queue に積む
$queue_a->enqueue(<データ>);
- Queue から引き抜いて $data に格納する
$data = $queue_a->dequeue();
//=======================================================...
* thread 間で共有する変数 [#e3cfa8b5]
- thread 間で共有したい変数は、その旨明示的に宣言する必要...
use threads;
use threads::shared; # thread 間で共有するデータを扱う
my <thread間共有変数> : shared = <初期化値>;
- 例
use threads;
use threads::shared; # thread 間で共有するデータを扱う
my $data : shared = 2; # 変数を初期化する値は、 ": sh...
my @datas : shared = ();
ページ名: