android bionic缺失pthread_cancel的解决方法

本文详细介绍了在Android NDK环境下,如何通过使用信号代替传统的pthread_cancel来取消worker线程,避免内存泄露和锁未释放的问题。并通过创建通讯管道与select函数结合的方式,实现worker线程的高效资源管理和优雅退出,适用于移动设备的高并发场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在native code中使用多线程好处多多,但是Android的bionic并没有完全实现标准POSIX线程库的所有API,例如pthread_cancel()。但是google这样做肯定有原因,被cancel的thread不一定已经把自己拥有的资源释放掉,因此很可能带来内存泄露,锁没有释放等问题。这些问题在移动设备上更加突出。


首先介绍一个指标的方法,使用signal替代cancel调用:

当worker thread超时时,在主线程(或者是监视进程)中调用

  1. if((status=pthread_kill(pthread_id,SIGUSR1))!=0)
  2. {
  3. printf("Errorcancellingthread%d,error=%d(%s)",pthread_id,status,strerrorstatus));
  4. }

在worker thread中加入对SIGUSR1信号的处理

  1. structsigactionactions;
  2. memset(&actions,0,sizeof(actions));
  3. sigemptyset(&actions.sa_mask);
  4. actions.sa_flags=0;
  5. actions.sa_handler=thread_exit_handler;
  6. rc=sigaction(SIGUSR1,&actions,NULL);
  7. voidthread_exit_handler(intsig)
  8. {
  9. printf("thissignalis%d\n",sig);
  10. pthread_exit(0);
  11. }
参考自: 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前先创建通讯管道

  1. if(pipe(mCtrlPipe)){
  2. SLOGE("pipefailed(%s)",strerror(errno));
  3. return-1;
  4. }
  5. if(pthread_create(&mThread,NULL,SocketListener::threadStart,this)){
  6. SLOGE("pthread_create(%s)",strerror(errno));
  7. return-1;
  8. }


2,在worker thread的大循环中使用select同时监控管道和IO fd

  1. while(1){//一般工作进程都带一个大循环
  2. FD_SET(mCtrlPipe[0],&read_fds);
  3. if(mCtrlPipe[0]>max)
  4. max=mCtrlPipe[0];
  5. if((rc=select(max+1,&read_fds,NULL,NULL,NULL))<0){
  6. SLOGE("selectfailed(%s)",strerror(errno));
  7. sleep(1);
  8. continue;
  9. }elseif(!rc)
  10. continue;
  11. //有人喊停了
  12. if(FD_ISSET(mCtrlPipe[0],&read_fds))
  13. break;
  14. }


3,需要退出时通过管道通知worker thread

  1. if(write(mCtrlPipe[1],&c,1)!=1){
  2. SLOGE("Errorwritingtocontrolpipe(%s)",strerror(errno));
  3. return-1;
  4. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值