android上Bionic不支持pthread_cancel等函数

谷歌在Bionic中未实现pthread_cancel等函数,因其实现复杂且收益有限。Bionic支持pthread_cleanup_push/pop用于资源清理。

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

谷歌在Bionic中没有实现pthread_cancel、pthread_testcancel的函数主要出于线程安全性以及其他api实现简洁性等方面考虑。官方的说法如下:

pthread_cancel():


   pthread_cancel() will *not* be supported in Bionic, because doing this would
   involve making the C library significantly bigger for very little benefit.

   Consider that:

     - A proper implementation must insert pthread cancellation checks in a lot
       of different places of the C library. And conformance is very difficult
       to test properly.

     - A proper implementation must also clean up resources, like releasing
       memory, or unlocking mutexes, properly if the cancellation happens in a
       complex function (e.g. inside gethostbyname() or fprintf() + complex
       formatting rules). This tends to slow down the path of many functions.

     - pthread cancellation cannot stop all threads: e.g. it can't do anything
       against an infinite loop

     - pthread cancellation itself has short-comings and isn't very portable
       (see  http://advogato.org/person/slamb/diary.html?start=49  for example).

   All of this is contrary to the Bionic design goals. If your code depends on
   thread cancellation, please consider alternatives.

   Note however that Bionic does implement pthread_cleanup_push() and
   pthread_cleanup_pop(), which can be used to handle cleanups that happen when
   a thread voluntarily exits through pthread_exit() or returning from its

   main function.


杀死线程的功能肯定是要用的,解决的方法大概有两种:

1.主进程中设标志,线程查询标志,被置位后调用pthread_exit()退出;

2.用pthread_kill()代替,如这位哥们所述http://www.rosoo.net/a/201112/15523.html,安全性很差


其他pthread相关资料:

http://www.cnblogs.com/lijunamneg/archive/2013/01/25/2877211.html

http://my.oschina.net/u/178323/blog/32535

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值