简体   繁体   English

Android NDK中的pthread_cancel()备选方案?

[英]pthread_cancel() alternatives in Android NDK?

I am porting a mid-sized body of C++ code to Android NDK. 我正在将一个中等大小的C ++代码移植到Android NDK。 Unfortunately the pthreads implementation (as of NDK v5, anyway) is incomplete. 不幸的是,pthreads实现(无论如何,NDK v5)都是不完整的。 Specifically, our application relies on pthread_cancel() to kill a worker thread. 具体来说,我们的应用程序依赖于pthread_cancel()来终止工作线程。 NDK does not implement pthread_cancel()! NDK没有实现pthread_cancel()! There are other obvious answers when the worker thread is responding normally. 当工作线程正常响应时,还有其他明显的答案。 But in cases where the worker thread is not responding (eg infinite loop), how can I cancel it without killing the whole process? 但是在工作线程没有响应的情况下(例如无限循环),如何在不杀死整个过程的情况下取消它?

Possible option that works for this guy: http://igourd.blogspot.com/2009/05/work-around-on-pthreadcancel-for.html 适用于这个人的可能选项: http//igourd.blogspot.com/2009/05/work-around-on-pthreadcancel-for.html

Reposting here in case: 在此重新发布:

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. 然后我使用pthread_kill触发SIG_USR1信号并使用信号处理程序退出此pthread并尝试它,它可以工作,但仍然想知道这种方法是否有任何缺点。

Timer out: 定时器输出:

if ( (status = pthread_kill(pthread_id, SIGUSR1)) != 0) 
{ 
    printf("Error cancelling thread %d, error = %d (%s)", pthread_id, status, strerror status));
} 

USR1 handler: 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);
}

Looks like the best answer is to rewrite so that threads aren't waiting on IO: http://groups.google.com/group/android-platform/browse_thread/thread/0aad393da2da65b1 看起来最好的答案是重写,以便线程不等待IO: http//groups.google.com/group/android-platform/browse_thread/thread/0aad393da2da65b1

I made a small library that take care of this. 我做了一个小型图书馆来照顾这个。

It exploits some unused bits of the bionic thread structure. 它利用仿生线程结构的一些未使用的位。

I called it libbthread :) 我叫它libbthread :)

Enjoy ;) 请享用 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM