- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下代码:
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/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!