bob*_*ski 8 perl multithreading
我正在尝试启动一个我可以随时暂停/恢复的线程.以下是我创建线程的方法:
use strict;
use warnings;
use threads;
use threads::shared;
use Thread::Suspend;
use Thread::Semaphore;
sub createThread {
my $semaphore = Thread::Semaphore->new();
my $thr = threads->create(sub {
local $SIG{KILL} = sub {
die "Thread killed\n";
};
local $SIG{STOP} = sub {
print "sig stop\n";
$semaphore->down();
};
local $SIG{CONT} = sub {
$semaphore->up();
print "sig cont\n";
};
my $test = sub {
while (1) {
$semaphore->down();
print STDERR "Working process\n";
sleep(2);
$semaphore->up();
}
};
return $test->();
});
return $thr->tid();
}
Run Code Online (Sandbox Code Playgroud)
检索线程ID(带return $thr->tid();)后.然后我尝试暂停它并sig stop正在打印消息,稍后当我尝试恢复它时sig cont不打印.这是暂停/恢复线程的代码:
sub pause {
my $class = shift;
my $threadId = shift;
my $thr = threads->object($threadId);
if ($thr) {
if ($thr->is_suspended() == 0) {
$thr->kill('STOP');
$thr->suspend();
return "Process $threadId paused\n";
} else {
return "Process $threadId has to be resumed\n";
}
} else {
return "Process $threadId not found\n";
}
}
sub resume {
my $class = shift;
my $threadId = shift;
my $thr = threads->object($threadId);
if ($thr) {
if ($thr->is_suspended() != 0) {
$thr->resume();
$thr->kill('CONT');
return "Operation $threadId resumed\n";
} else {
return "Operation $threadId has not been paused\n";
}
} else {
return "Process $threadId not found\n";
}
}
Run Code Online (Sandbox Code Playgroud)
在我恢复暂停的线程后,消息Operation X was resumed却sig cont没有,并且线程功能也没有被恢复.
目前还不清楚你是否需要Thread::Semaphore一个单独的目的,但它不是必需的Thread::Suspend
我怀疑你的暂停/恢复不起作用的原因是你已经覆盖Thread::Suspend了为自己的目的设置的信号处理程序
如果我删除所有信号处理程序和Thread::Semaphore东西,那么你的代码工作正常:
use strict;
use warnings 'all';
use threads;
use Thread::Suspend;
STDOUT->autoflush;
my $tid = create_thread();
for ( 1 .. 10 ) {
sleep 5;
print pause($tid);
sleep 5;
print resume($tid);
}
sub create_thread {
my $thr = threads->create( sub {
while () {
print "Working thread\n";
sleep 1;
}
} );
return $thr->tid;
}
sub pause {
my ($tid) = @_;
my $thr = threads->object($tid);
return "Thread $tid not found\n" unless $thr;
return "Thread $tid is already suspended\n" if $thr->is_suspended;
$thr->suspend;
return "Thread $tid paused\n";
}
sub resume {
my ($tid) = @_;
my $thr = threads->object($tid);
return "Thread $tid not found\n" unless $thr;
return "Thread $tid has not been paused\n" unless $thr->is_suspended;
$thr->resume;
return "Thread $tid resumed\n";
}
Run Code Online (Sandbox Code Playgroud)
Working thread
Working thread
Working thread
Working thread
Working thread
Thread 1 paused
Thread 1 resumed
Working thread
Working thread
Working thread
Working thread
Working thread
Thread 1 paused
Thread 1 resumed
Working thread
Working thread
...
Run Code Online (Sandbox Code Playgroud)
您的子程序也没有实际需要.这是一个简单的实现
use strict;
use warnings 'all';
use threads;
use Thread::Suspend;
STDOUT->autoflush;
sub thread_sub {
while () {
printf "Working thread %d\n", threads->self->tid;
sleep 1;
}
}
my $thr = threads->create(\&thread_sub);
for ( 1 .. 10 ) {
sleep 5;
if ( my $suspended = $thr->suspend ) {
printf "Thread %d suspended\n", $suspended->tid;
}
sleep 5;
if ( my $resumed = $thr->resume ) {
printf "Thread %d resumed\n", $resumed->tid;
}
}
Run Code Online (Sandbox Code Playgroud)
Working thread 1
Working thread 1
Working thread 1
Working thread 1
Working thread 1
Working thread 1
Thread 1 suspended
Thread 1 resumed
Working thread 1
Working thread 1
Working thread 1
Working thread 1
Working thread 1
Working thread 1
Thread 1 suspended
Thread 1 resumed
Working thread 1
Working thread 1
Working thread 1
Run Code Online (Sandbox Code Playgroud)