繁体   English   中英

终止在读取时阻塞的线程c ++ 11

[英]Terminate thread c++11 blocked on read

我有以下代码:

class Foo {
private:
    std::thread thread;
    void run();
    std::atomic_flag running;
    std::thread::native_handle_type native;
public:
    Foo(const std::string& filename);
    virtual ~Foo();
    virtual void doOnChange();
    void start();
    void quit();
};

#include "Foo.h"
#include <functional>

#include <iostream>

Foo::Foo(const std::string& filename) :
        thread(), running(ATOMIC_FLAG_INIT) {
    file = filename;
    native = 0;
}

Foo::~Foo() {
    quit();
}

void Foo::start() {
    running.test_and_set();
    try {
        thread = std::thread(&Foo::run, this);
    } catch (...) {
        running.clear();
        throw;
    }
    native = thread.native_handle();
}

void Foo::quit() {
    running.clear();
    pthread_cancel(native);
    pthread_join(native, nullptr);
    //c++11-style not working here
    /*if (thread.joinable()) {
        thread.join();
        thread.detach();
    }*/
}

void Foo::run() {
   while (running.test_and_set()) {
        numRead = read(fd, buf, BUF_LEN);
        .....bla bla bla.......
   }
}

我试图在我的程序清理代码中退出此线程。 使用pthread工作,但我想知道我是否可以用c ++ 11做更好的事情(没有原生句柄)。 在我看来,使用c ++ 11代码处理所有情况都没有好办法。 正如您在此处所见,线程在读取系统调用时被阻止。 因此,即使我清除了该标志,该线程仍将被阻止,并且加入呼叫将永久阻止。 所以我真正需要的是一个中断(在这种情况下是pthread_cancel )。 但是如果我调用pthread_cancel我就不pthread_cancel调用c ++ 11 join()方法,因为它失败了,我只能调用pthread_join() 所以看起来这个标准有一个非常大的限制,我错过了什么吗?

编辑:

经过下面的讨论,我改变了用std :: atomic替换std :: atomic_flag并使用信号处理程序的Foo类实现。 我使用了信号处理程序,因为在我看来最好有一个普通的基类,在基类中使用自管道技巧太难了,逻辑应该委托给孩子。 最终实施:

#include <thread>
#include <atomic>

class Foo {
private:
    std::thread thread;
    void mainFoo();
    std::atomic<bool> running;
    std::string name;
    std::thread::native_handle_type native;
    static void signalHandler(int signal);
    void run();
public:
    Thread(const std::string& name);
    virtual ~Thread();
    void start();
    void quit();
    void interrupt();
    void join();
    void detach();
    const std::string& getName() const;
    bool isRunning() const;
};

Cpp文件:

#include <functional>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <Foo.h>
#include <csignal>
#include <iostream>

Foo::Foo(const std::string& name) :
        name(name) {
    running = false;
    native = 0;
    this->name.resize(16, '\0');
}

Foo::~Foo() {
}

void Foo::start() {
    running = true;
    try {
        thread = std::thread(&Foo::mainFoo, this);
    } catch (...) {
        running = false;
        throw;
    }
    native = thread.native_handle();
    pthread_setname_np(native, name.c_str());
}

void Foo::quit() {
    if (running) {
        running = false;
        pthread_kill(native, SIGINT);
        if (thread.joinable()) {
            thread.join();
        }
    }
}

void Foo::mainFoo() {
 //enforce POSIX semantics
 siginterrupt(SIGINT, true);
 std::signal(SIGINT, signalHandler);
    run();
    running = false;
}

void Foo::join() {
    if (thread.joinable())
        thread.join();
}

void Foo::signalHandler(int signal) {
}

void Foo::interrupt() {
    pthread_kill(native, SIGINT);
}

void Foo::detach() {
    if (thread.joinable())
        thread.detach();
}

const std::string& Foo::getName() const {
    return name;
}

bool Foo::isRunning() const {
    return running;
}

void Foo::run() {
    while(isRunning()) {
         num = read(.....);
         //if read is interrupted loop again, this time
         //isRunning() will return false
    }
}

正如您在此处所见,线程在读取系统调用时被阻止。 因此,即使我清除了该标志,该线程仍将被阻止,并且加入呼叫将永久阻止。

解决这个问题的方法是 std ::引发 一个信号,例如SIGINT Edit:你需要使用pthread_kill来提升信号,这样信号就会被正确的线程处理。 正如您可以从手册中读到的那样, 读取会被信号中断。 您必须处理std :: signal ,否则整个过程将提前终止。

在使用BSD信号处理而不是POSIX的系统上,系统调用默认重新启动而不是在中断时失败。 我建议的方法依赖于POSIX行为,其中调用设置EINTR并返回。 可以使用siginterrupt显式设置POSIX行为。 另一种选择是使用sigaction注册信号处理程序,除非由标志指定,否则不会重新启动。

read中断后,必须在重试读取之前检查线程是否应该停止。

使用c ++ 11(甚至可能没有它)不要在线程中调用任何阻塞系统调用

调用阻塞系统调用就好了。 如果您希望在不终止进程的情况下终止线程(在有限时间内),则不应该执行可能无限期长时间阻塞的不可中断系统调用。 在我的头顶,我不知道是否有任何系统调用匹配这样的描述。

一个最小的例子(完成除了无限期阻塞read 。你可以使用sleep(100000)来模拟它):

#include <thread>
#include <iostream>
#include <csignal>
#include <cerrno>
#include <unistd.h>

constexpr int quit_signal = SIGINT;
thread_local volatile std::sig_atomic_t quit = false;

int main()
{
    // enforce POSIX semantics
    siginterrupt(quit_signal, true);

    // register signal handler
    std::signal(quit_signal, [](int) {
        quit = true;
    });

    auto t = std::thread([]() {
        char buf[10];
        while(!quit) {
            std::cout << "initiated read\n";
            int count = read(some_fd_that_never_finishes, buf, sizeof buf);
            if (count == -1) {
                if (errno == EINTR) {
                    std::cout << "read was interrupted due to a signal.\n";
                    continue;
                }
            }
        }
        std::cout << "quit is true. Exiting\n";;
    });

    // wait for a while and let the child thread initiate read
    sleep(1);

    // send signal to thread
    pthread_kill(t.native_handle(), quit_signal);

    t.join();
}

强行杀死一个线程通常是一个非常糟糕的主意,特别是在C ++中,这可能是std::thread API没有为它提供接口的原因。

如果你真的想杀死一个执行线程 - 在这种情况下这是不必要的,因为你可以安全地中断系统调用 - 那么你应该使用子进程而不是子线程。 杀死子进程不会破坏父进程的堆。 也就是说,C ++标准库不提供进程间API。

正如其他人所说,杀死一个正在运行的线程是一个坏主意™。

但是,在这种情况下,您以某种方式知道线程在读取时阻塞,并希望它停止。

这样做的一个简单方法是使用“自管技巧”。 打开一个管道,并在select()poll()调用上使用线程块,检查管道的读取结束和正在读取的文件描述符。 如果希望线程停止,请将单个字节写入写入描述符。 线程唤醒,看到管道上的字节,然后可以终止。

这种方法避免了完全杀死线程的未定义行为,允许您使用阻塞系统调用来避免轮询并响应终止请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM