Android NDK 中的 pthread_cancel() 替代品?
-
25-09-2019 - |
题
我正在将中等大小的 C++ 代码移植到 Android NDK。不幸的是,pthreads 实现(无论如何,从 NDK v5 开始)是不完整的。具体来说,我们的应用程序依赖 pthread_cancel() 来终止工作线程。NDK 没有实现 pthread_cancel()!当工作线程正常响应时,还有其他明显的答案。但在工作线程没有响应的情况下(例如无限循环),如何在不终止整个进程的情况下取消它?
解决方案
可能适合这个人的选项: http://igourd.blogspot.com/2009/05/work-around-on-pthreadcancel-for.html
在此重新发布以防万一:
然后,我使用pthread_kill触发sig_usr1信号,并使用信号处理程序退出此pthread并尝试过,但仍然想知道这种方法是否有任何缺点。
定时器超时:
if ( (status = pthread_kill(pthread_id, SIGUSR1)) != 0)
{
printf("Error cancelling thread %d, error = %d (%s)", pthread_id, status, strerror status));
}
USR1 处理程序:
struct sigaction actions;
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = thread_exit_handler;
rc = sigaction(SIGUSR1,&actions,NULL);
void thread_exit_handler(int sig)
{
printf("this signal is %d \n", sig);
pthread_exit(0);
}
看起来最好的答案是重写,这样线程就不会等待 IO: http://groups.google.com/group/android-platform/browse_thread/thread/0aad393da2da65b1
其他提示
不隶属于 StackOverflow