- #!/usr/bin/perl -w
- # 装载perl线程模块
- use threads;
- use POSIX qw(strftime);
- sub produce {
- my $thread_name = shift;
- while(1) {
- my $r = int(rand(100));
- # 获得本线程内当前时间
- my $now_time = strftime "%Y-%m-%d %H:%M:%S", localtime;
- # 获得本线程的ID
- $n = threads->tid();
- my $r2 = int(rand(3));
- printf("$thread_name \$random: %-2s | \$random2: $r2 | thread_id: $n | $now_time |\n",$r);
- sleep($r2);
- }
- }
- # 创建N个线程,每个线程调用 &produce 子例程,并传送一个参数.
- my $jason1 = threads->create(\&produce, "jason1");
- my $jason2 = threads->create(\&produce, "jason2");
- my $jason3 = threads->create(\&produce, "jason3");
- my $jason4 = threads->create(\&produce, "jason4");
- my $jason5 = threads->create(\&produce, "jason5");
- # 线程结束,收割.
- $jason1->join();
- $jason2->join();
- $jason3->join();
- $jason4->join();
- $jason5->join();
use threads;模块的详细使用方法还要好好研究.
http://search.cpan.org/~jdhedden/threads-1.82/lib/threads.pm
注:
之前看了段时间的Thread模块.后来才发现.这个模块已经被放弃了.因为有很多问题.
打算用perl写多线程,还是好好研究threads.