终止读取时阻止的线程 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
我不能再调用 c++11 join(( 方法,因为它失败了,我只能调用 pthread_join()
.所以似乎标准有一个很大的限制,我错过了什么吗?
编辑:
在下面的讨论之后,我更改了 Foo 类实现,将 std::atomic_flag 替换为 std::atomic 并使用信号处理程序。我使用信号处理程序是因为在我看来最好有一个通用的基类,在基类中使用自管道技巧太难了,逻辑应该委托给孩子。最终实现:
#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, ' ');
}
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::raise 一个信号,例如 SIGINT
编辑:您需要使用 pthread_kill 发出信号,以便信号将由正确的线程处理。正如您可以从手册中读取的那样,读取被信号中断。您必须处理 std::signal,否则整个过程将提前终止。
在使用 BSD 信号处理而不是 POSIX 的系统上,系统调用默认重新启动,而不是在中断时失败。我建议的方法依赖于 POSIX 行为,其中调用设置EINTR
并返回。POSIX 行为可以使用 siginterrupt 显式设置。另一种选择是使用 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 readn";
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. Exitingn";;
});
// 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()
调用上使线程块,检查管道的读取端和正在读取的文件描述符。如果希望线程停止,请将单个字节写入写入描述符。线程唤醒,看到管道上的字节,然后可以终止。
这种方法避免了直接杀死线程的未定义行为,允许您使用阻塞系统调用来避免轮询,并响应终止请求。
- 具有 C++11 多线程的特征库
- 为什么即使调用了析构函数,C++11 中的分离线程也可以执行
- C++11:具有互斥锁的线程看到原子变量的值发生变化,尽管这是唯一可以改变它的代码
- C++11:将矢量元素作为线程传递到线程函数中
- DirectX 11 -- Hello Triangle 未呈现 -- 原因:运行时库C++多线程
- 终止读取时阻止的线程 c++11
- C++11如何在1个线程中使用条件变量处理2个线程安全队列
- C++11 - 可以等待多个不同事件的线程?
- 将字符串指针传递到C++和Xcode 11.1中不同线程上运行的函数
- 多线程 C++11 应用程序中的同步
- std::线程与 pthread_setschedparam 与 C++ 11.
- C++ 11 thread_local和"外来"线程
- C++ 11 线程在分离后会自动销毁吗?
- Valgrind 检查简单多线程 C++11 得到 SIG11 错误
- Boost::线程 / C++11 std::thread,想要唤醒工作线程的条件
- c++将各种参数传递给父类构造函数(线程c++11)
- 线程c++11标准和Netbeans/其他IDE
- 多线程c++11-ish队列在windows上失败
- 在多线程c++ 11程序中,当异常未处理时会发生什么
- 分段故障多线程C++11