在native code中使用多线程好处多多,但是Android的bionic并没有完全实现标准POSIX线程库的所有API,例如pthread_cancel()。但是google这样做肯定有原因,被cancel的thread不一定已经把自己拥有的资源释放掉,因此很可能带来内存泄露,锁没有释放等问题。这些问题在移动设备上更加突出。
首先介绍一个指标的方法,使用signal替代cancel调用:
当worker thread超时时,在主线程(或者是监视进程)中调用
-
if((status=pthread_kill(pthread_id,SIGUSR1))!=0)
-
{
-
printf("Errorcancellingthread%d,error=%d(%s)",pthread_id,status,strerrorstatus));
-
}
在worker thread中加入对SIGUSR1信号的处理
-
structsigactionactions;
-
memset(&actions,0,sizeof(actions));
-
sigemptyset(&actions.sa_mask);
-
actions.sa_flags=0;
-
actions.sa_handler=thread_exit_handler;
-
rc=sigaction(SIGUSR1,&actions,NULL);
-
voidthread_exit_handler(intsig)
-
{
-
printf("thissignalis%d\n",sig);
-
pthread_exit(0);
-
}
参考自:http://stackoverflow.com/questions/4610086/pthread-cancel-alternatives-in-android-ndk
最根本的解决方法是重写worker thread,使用poll或者select等处理IO操作防止stuck的发生,下面是Android源码system/libsysutils/src/SocketListener.cpp的处理方法
1,创建worker thread前先创建通讯管道
-
if(pipe(mCtrlPipe)){
-
SLOGE("pipefailed(%s)",strerror(errno));
-
return-1;
-
}
-
-
if(pthread_create(&mThread,NULL,SocketListener::threadStart,this)){
-
SLOGE("pthread_create(%s)",strerror(errno));
-
return-1;
-
}
2,在worker thread的大循环中使用select同时监控管道和IO fd
-
while(1){
-
FD_SET(mCtrlPipe[0],&read_fds);
-
if(mCtrlPipe[0]>max)
-
max=mCtrlPipe[0];
-
if((rc=select(max+1,&read_fds,NULL,NULL,NULL))<0){
-
SLOGE("selectfailed(%s)",strerror(errno));
-
sleep(1);
-
continue;
-
}elseif(!rc)
-
continue;
-
-
-
if(FD_ISSET(mCtrlPipe[0],&read_fds))
-
break;
-
}
3,需要退出时通过管道通知worker thread
-
if(write(mCtrlPipe[1],&c,1)!=1){
-
SLOGE("Errorwritingtocontrolpipe(%s)",strerror(errno));
-
return-1;
-
}
分享到:
相关推荐
32位android中bionic是32位的,其中的mutex只有一半也就是16位能够存储pid,当通过docker运行android时,大概率pid会超过16位的范围,就可能会导致android中mutex死锁,表现为应用卡住黑屏。 [32-bit ABI bugs]...
Armbian_5.76_Aml-s905_Ubuntu_bionic_default_4.20.5_20190224.img yadi.sk/d/pHxaRAs-tZiei/官网下载的
Armbian_5.99_Aml-g12_Ubuntu_bionic_default_5.3.0,带wifi驱动rtl8189etv\es
可用于 Amlogic S905 芯片 N1刷机
Armbian_5.44_S9xxx_Ubuntu_bionic_3.14.29_server_20180729.img.xz 数码视讯Q5 armbian系统镜像,测试正常
Armbian_5.65_Orangepiplus_Ubuntu_bionic_next_4.14.78.7z
Ubuntu 完全教程
Bionic
libc from google android even smaller than uclibc
This is the download area of the openSUSE distribution and the openSUSE Build Service. If you are searching for a specific package for your distribution, we recommend to use our Software Portal ...
详细介绍了ubuntu server的基础知识,想要对ubuntu server了解的朋友,此书绝对是一本好书
android 收音机 FM 驱动 hal层 框架层以及应用层代码 方法一 不需要framework部分 1.fm放到 \hardware\rk2x 2.FmRadio 放到 packages\apps 3.hardware\libhardware\include\hardware\fm.h 放到hardware\libhardware...
第五章_ARM9_IO和IO操作.ppt第五章_ARM9_IO和IO操作.ppt第五章_ARM9_IO和IO操作.ppt第五章_ARM9_IO和IO操作.ppt第五章_ARM9_IO和IO操作.ppt第五章_ARM9_IO和IO操作.ppt第五章_ARM9_IO和IO操作.ppt第五章_ARM9_IO和IO...
wkhtmltox_0.12.5-1.bionic_amd64.zip为ubuntu18.04 64位 odoo12 环境 解压后使用 wkhtmltopdf是odoo开源ERP系统的依赖库 官方下载链接地址奇慢无比,甚至不可用,可使用本链接,不推荐使用官方的
处理收集的数据格式
Ubuntu自带的FTP服务器是vsftpd. 安装使用 1、安装vsftpd sudo apt-get install vsftpd 安装了之后会在/home/下建立一个ftp目录。这时候你可以试着访问下ftp://IP地 址。应该可以看到一个空白内容的ftp空间。...
armbian笔记—N1盒子刷入armbian系统并优化 近期又和小伙伴一块上车N1盒子,开始了我们对N1的折腾; 这篇文章主要叙述了如何将armbian——Ubuntu系统刷入并优化N1; 欢迎感兴趣的发烧友翻看; 本人水平有限,文章...
一类仿生优化算法,只需要改待优化函数就行了
适用于ubuntu18的virtualbox virtualbox-6.1_6.1.6-137129~Ubuntu~bionic_amd64.0.deb
Ubuntu64位编译Android源码出错解决办法