#!/usr/bin/perl
use
threads (
'yield'
,
'stack_size'
=> 64*4096,
'exit'
=>
'threads_only'
,
'stringify'
);
sub
start_thread {
my
@args
=
@_
;
print
(
'Thread started: '
,
join
(
' '
,
@args
),
"\n"
);
}
##创建线程的方法
# my $thr = threads->create('func_name', ...);
# my $thr = threads->create(sub { ... }, ...);
# my $thr = threads->create(\&func, ...);
# The "->new()" method is an alias for "->create()".
my
$thr
= threads->create(
'start_thread'
,
'argument1'
,
'argument2'
);
#通过create创建线程。返回线程实例
$thr
->
join
();
#等待线程结束
threads->create(
sub
{
print
(
"I am a thread\n"
); })->
join
();
#创建一个线程,没有返回值。那这个线程实例如何访问呢?
my
$thr2
= async {
foreach
(
@ARGS
) {
print
"$_\n"
; } };
#通过async使用匿名子例程创建线程
$thr2
->
join
();
if
(
my
$err
=
$thr2
->error()) {
请发表评论