gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:01:49 31 4
gpt4 key购买 nike

我有以下代码:

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(无 native 句柄)做一些更好的事情。在我看来,没有什么好的方法可以使用 c++11 代码来处理所有情况。正如您在此处看到的,线程在读取系统调用时被阻塞。因此,即使我清除标志,线程仍将被阻塞,并且 join 调用将永远阻塞。所以我真正需要的是一个中断(在本例中为 pthread_cancel)。但是,如果我调用 pthread_cancel,我将无法再调用 c++11 join() 方法,因为它失败了,我只能调用 pthread_join()。所以看起来标准有很大的限制,我错过了什么吗?

编辑:

在下面的讨论之后,我更改了 Foo 类实现,将 std::atomic_flag 替换为 std::atomic 并使用信号处理程序。我使用信号处理程序是因为在我看来最好有一个通用的基类,在基类中使用自管道技巧太难了,逻辑应该委托(delegate)给 child 。最终实现:

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

最佳答案

As you can see here the thread is blocked on a read system call. So even if I clear the flag the thread will be still blocked and join call will block forever.

解决方法是 std::raise 信号,例如 SIGINT 编辑:您需要使用 pthread_kill 提高信号这样信号就会被正确的线程处理。正如您可以从手册中读到的那样,read被信号中断。您必须处理 std::signal否则整个过程将提前终止。

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

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

using c++11 (maybe even without it) don't call any blocking system call in the thread

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

一个最小的例子(除了无限期阻塞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 不为其提供接口(interface)的原因。

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

关于c++ - 终止线程 c++11 在读取时被阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51742179/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com