【发布时间】:2022-07-08 02:03:38
【问题描述】:
我正在尝试编写一个简单的脚本来生成一个执行可能超时的任务的线程。 (为了给 StackOverflow 写一个简单的例子,我用sleep 命令替换了实际过程)。
这个程序产生一个线程,然后使用cond_timedwait 来监视线程并检查它是否超时。如果发生超时,它会调用线程上的kill 方法并使用“STOP”信号通知线程它应该退出。
use strict;
use threads;
use threads::shared;
use warnings;
my $var :shared;
my $thread = threads->create(sub {
# Tell the thread how to handle the STOP signal
local $SIG{'STOP'} = sub {
print "Stop signal received\n";
threads->exit();
};
# Perform a process that takes some time
sleep 10;
# Signal that the thread is complete
lock($var); cond_signal($var);
});
# Current time + 1 second
my $wait_time = time() + 1;
my $timeout;
{
# Wait for the thread to complete or until a timeout has occurred
lock($var); $timeout = !cond_timedwait($var, $wait_time);
}
# Check if a timeout occurred
if ($timeout) {
print "A timeout has occurred\n";
# Signal the thread to stop
$thread->kill('STOP')->join();
}
else {
$thread->join();
}
此代码运行成功并打印以下输出:
1 秒过去了...
A timeout has occurred
9 秒过去了...
Stop signal received
问题是,即使检测到超时并将“STOP”信号发送到线程,程序似乎仍在等待整整 10 秒,然后才打印“Stop signal received”并退出。
我尝试更改它,使其在终止线程后调用detach 而不是join,但是从未打印过“收到停止信号”消息,这意味着程序在线程干净退出之前退出。我想确保线程实际上被中断并退出,因为在实际程序中,我想在超时发生后杀死并重试该进程,如果已经在分离线程上运行另一个实例,该进程将无法工作.
我怎样才能使线程在收到“STOP”信号时立即打印消息并退出?
【问题讨论】:
标签: windows multithreading perl timeout signals