pthread_cancel可以()购买Android NDK的选择吗? [英] pthread_cancel() alternatives in Android NDK?
问题描述
我移植的C ++ code的中型体的Android NDK。不幸的是,pthreads实现(如NDK V5,反正)是不完整的。具体来说,我们的应用程序依赖于pthread_cancel可以()杀死一个工作线程。 NDK没有实现pthread_cancel可以()!有些情况下,工作线程正常响应等明显的答案。但在情况下,工作线程没有响应(例如无限循环),我怎么能取消它不会杀死的全过程?
I am porting a mid-sized body of C++ code to Android NDK. Unfortunately the pthreads implementation (as of NDK v5, anyway) is incomplete. Specifically, our application relies on pthread_cancel() to kill a worker thread. NDK does not implement pthread_cancel()! There are other obvious answers when the worker thread is responding normally. But in cases where the worker thread is not responding (e.g. infinite loop), how can I cancel it without killing the whole process?
推荐答案
这是这家伙的作品可能的选择:<一href=\"http://igourd.blogspot.com/2009/05/work-around-on-pthreadcancel-for.html\">http://igourd.blogspot.com/2009/05/work-around-on-pthreadcancel-for.html
Possible option that works for this guy: http://igourd.blogspot.com/2009/05/work-around-on-pthreadcancel-for.html
转贴在这里情况:
然后我用pthread_kill触发
SIG_USR1信号和使用信号处理程序
退出该pthread的和试了一下,
工作原理,但如果有的话仍然不知道
缺点对于这种方法。
Then I use pthread_kill to trigger a SIG_USR1 signal and use signal handler to exit this pthread and tried it, it works, but still wondering if any drawbacks for this kind of method.
定时器出来:
if ( (status = pthread_kill(pthread_id, SIGUSR1)) != 0)
{
printf("Error cancelling thread %d, error = %d (%s)", pthread_id, status, strerror status));
}
USR1处理程序:
USR1 handler:
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:<一href=\"http://groups.google.com/group/android-platform/browse_thread/thread/0aad393da2da65b1\">http://groups.google.com/group/android-platform/browse_thread/thread/0aad393da2da65b1
这篇关于pthread_cancel可以()购买Android NDK的选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!