我正在尝试启动一个可以随时暂停/恢复的线程。这是我创建线程的方法:

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();
}

检索线程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";
    }
}

在我恢复一个暂停的线程之后,消息Operation X was resumedsig 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";
}

输出

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
...



更新

也不真正需要您的子例程。这是一个简单的实现

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;
    }
}

输出

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

关于multithreading - Perl恢复线程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38611566/

10-13 09:36