multithreading - Perl 和 Curses 库 - 从子线程退出

标签 multithreading perl ncurses

sub worker {

    # Curse stuff
    initscr();
    cbreak();
    noecho();

    my $fh = FileHandle->new;
    open $fh, q{-|},
        "$myexe @pre_args make @after_args 2>&1"
        or croak 'Cannot open';
    process_output($fh);
    my $err = close $fh;

    endwin();
    return;
} 
sub process_output {
    my ($fh) = @_;

    while (my $line = <$fh>) {
        #do stuff
    }
}
ReadMode 3;
threads->create(\&worker);
while (threads->list(threads::running)) {
    my $char = ReadKey -1, *STDIN;
    if ($char) {
        if ($char eq 'q') {
            endwin();
            kill('INT', $$);
            threads->exit();
        }
    }
}
ReadMode 0;

foreach my $thr (threads->list) {
    $thr->join();

当我按“q”时:

Perl exited with active threads:
        1 running and unjoined
        0 finished and unjoined
        0 running and detached

然后我做了ps -fu myuserid

我看到$myexe仍在运行

Q1) 如何强制子进程退出?线程->exit() 似乎不起作用

最佳答案

示例程序最明显的问题是它对curses 库使用了多线程。那是行不通的。 (curses 不是线程安全的)。如果必须这样做,请将所有诅咒保持在同一线程中。

关于multithreading - Perl 和 Curses 库 - 从子线程退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28647933/

相关文章:

c - ncurses 中的窗口定位

c - ncurses 程序在 PuTTY 以外的任何东西上崩溃

django - cherrypy 如何处理用户线程?

正则表达式匹配 "not preceded by, unless followed by"

Perl删除目录中的所有文件

perl - 如何通过解析 WSDL 文件直接创建 SOAP 请求?

c - C ncurses 应用程序中的希伯来语支持

multithreading - 每个核心有多少个线程?

java - Java 中的线程最大数量?

java - 为什么我的 TCP 服务器套接字在一个客户端失去连接后关闭?