- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在将之前围绕 pthreads 的线程包装器转换为 std::thread。但是 c++11 没有办法取消线程。尽管如此,我还是需要取消线程,因为它们可能正在外部库中执行非常冗长的任务。
我正在考虑在我的平台中使用给我 pthread_id 的 native_handle。我在 Linux (Ubuntu 12.10) 中使用 gcc 4.7。这个想法是:
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main(int argc, char **argv) {
cout << "Hello, world!" << endl;
auto lambda = []() {
cout << "ID: "<<pthread_self() <<endl;
while (true) {
cout << "Hello" << endl;
this_thread::sleep_for(chrono::seconds(2));
}
};
pthread_t id;
{
std::thread th(lambda);
this_thread::sleep_for(chrono::seconds(1));
id = th.native_handle();
cout << id << endl;
th.detach();
}
cout << "cancelling ID: "<< id << endl;
pthread_cancel(id);
cout << "cancelled: "<< id << endl;
return 0;
}
线程被 pthreads 抛出的异常取消。
我的问题是:
这种做法会不会有什么问题(除了不可移植)?
最佳答案
不,我认为您不会遇到以下问题:
例如,标准说当一个线程结束时,变量将被销毁。如果您取消一个线程,这对编译器来说将更加困难,如果不是不可能的话。
因此,我建议不要取消一个线程,如果你能以某种方式避免的话。编写一个标准的轮询循环、使用条件变量、监听中断读取的信号等等——并定期结束线程。
关于c++ - 使用 native_handle() + pthread_cancel() 取消 std::thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13531241/
我正在使用 ndk r9b 版本在 android 中构建 ffmpeg 2.1.3。 我使用了下面的命令。 ./configure --enable-shared --prefix=/home/fb
c 函数 pthread_cancel() 可以阻塞还是总是立即返回?我的手册页指出: The above steps happen asynchronously with respect to th
您好,我有一个关于取消使用互斥锁和条件变量的线程的问题。该线程已延迟取消类型。当我只使用函数pthread_mutex_lock/unlock 和pthread_cond_wait 时,一个取消请求到
我正在阅读 Kerrisk's book并看到以下内容作为注释, Caution is required when using a cast integer as the return value o
这个问题已经有答案了: pthread_cancel() function failed to terminate a thread (2 个回答) 已关闭 8 年前。 我在主线程中创建并连接了 2
我开发了一个程序,它在开始时创建一个线程。然后我想在线程运行时使用 pthread_cancel() 终止线程。 但是如果我这样做,我就会崩溃。这种崩溃并不总是能够重现。有时程序会避免崩溃。 我发现如
当我的进程执行完毕时,我想使用 pthread_cancel 取消我的“计时器”线程。基本上我们正在将一个修改后的终端作为一个项目进行。当我在一个终端中调用 sleep 300 时,我希望能够打开另一
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
我正在用我的 C 测试代码测试 pthread_cancel() 功能。这不是我正在使用的确切代码,而是用于解释的简化形式。 int main(argc, char *argvp[]) { w
我有一个程序试图通过已实现的池使用创建和取消。 创建如下: int threadsNum=10; while (createdid = created; st->t = newThread;
我有一个程序试图通过已实现的池使用创建和取消。 创建如下: while (createdid = created; st->t = &newThread; pthread_mutex_
在我的程序中,我有两个线程,一个计算一些递归函数,另一个在第一个线程进行计算时显示“进度点”。有代码: #include #include #include #include #include
我的问题类似于this one .但是看完它的所有答案,我仍然不知道pthread_cancel()能得到什么样的安全保证。所以我想问一个更具体的问题: 假设 pthread_cancel() 是在一
我正在使用 pthread_create() 和 pthread_cancel() 函数来创建多线程程序,但我注意到 pthread_cancel() 确实没有真正终止它应该终止的线程。 void c
我正在学习 pthread_cancel 函数并测试线程是否会在未达到取消点时被取消。线程由默认属性创建,并使其在添加循环中运行。但是当发送取消请求并且线程立即退出时。它没有达到取消点,我认为它不应该
大家好,我有一些关于线程的问题,例如我有一些线程 1 分配了一些内存,另一个线程(假设 2)是使用 pthread_cancel() 或仅使用 return 杀死线程 1 它分配的内存和平发生了什么?
我使用的 pthread 不分配任何局部变量。由于我不会在这里讨论的原因,我需要一个 pthread_cancel() 选项,并且我正在编写的线程应该能够支持它(没有资源需要清理,可以在以下位置停止执
我正在尝试从主线程中取消一个线程。它不会立即取消,但大约需要 4 秒。应该是因为我已将其取消类型设置为与默认(延迟)异步。为什么? #include #include #include #inc
我现在正在维护一些代码,它使用 pthread_create() 创建一个 pthread。此线程还在创建后不久调用 pthread_detach(pthread_self());。此外,它使用 pt
我有一个程序应该启动一个线程。为了避免退出软件,线程在无限循环中运行,我加入了线程。这个线程永远不应该返回一个值。所以现在我有一个问题,在我调用 pthread_cancel valgrind 后检测
我是一名优秀的程序员,十分优秀!