gpt4 book ai didi

c++ - Android NDK 中的 pthread_cancel() 替代方案?

转载 作者:IT老高 更新时间:2023-10-28 21:53:03 31 4
gpt4 key购买 nike

我正在将中型 C++ 代码体移植到 Android NDK。不幸的是,pthreads 实现(无论如何,从 NDK v5 开始)是不完整的。具体来说,我们的应用程序依赖 pthread_cancel() 来终止工作线程。 NDK 没有实现 pthread_cancel()!当工作线程正常响应时,还有其他明显的答案。但是在工作线程没有响应的情况下(例如无限循环),我怎样才能在不杀死整个进程的情况下取消它?

最佳答案

适用于此人的可能选项:http://igourd.blogspot.com/2009/05/work-around-on-pthreadcancel-for.html

在此转发以防万一:

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 处理程序:

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

关于c++ - Android NDK 中的 pthread_cancel() 替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4610086/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com