谷歌在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