pthread_join / __pthread_internal_find 函数发生SIGABRT的crash (Android)

Posted wen_rc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pthread_join / __pthread_internal_find 函数发生SIGABRT的crash (Android)相关的知识,希望对你有一定的参考价值。

pthread_join等待一个线程结束才返回。他会先调用__pthread_internal_find函数查找这个线程是否存在,但在android 26之后,__pthread_internal_find函数对找不到的线程会crash。

https://stackoverflow.com/questions/46457800/android-oreo-8-0-native-c-crash-invalid-pthread-t-passed-to-libc
https://github.com/aosp-mirror/platform_bionic/blob/master/libc/bionic/pthread_internal.cpp#L104

我这边看日志的确有这行async_safe_fatal(“invalid pthread_t %p passed to %s”, thread, caller);

那怎么办呢?第一想法就是去搜索:如何判断线程是否存在

搜到的方法是用thread_kill,但是看实现,thread_kill也会调用__pthread_internal_find啊。怎么办?

https://github.com/aosp-mirror/platform_bionic/blob/92c6f7ee9014f434fbcce89ab894c745e36732d2/libc/bionic/pthread_kill.cpp#L38
https://github.com/aosp-mirror/platform_bionic/search?q=pthread_gettid_np&unscoped_q=pthread_gettid_np


还有一种思路,或者说这才是正确的解决办法,就是找到哪些地方调用了join,不要对一个线程重复调用就行了。

拓展

A thread may either be joinable or detached. If a thread is
joinable, then another thread can call pthread_join(3) to wait for
the thread to terminate and fetch its exit status. Only when a
terminated joinable thread has been joined are the last of its
resources released back to the system. When a detached thread
terminates, its resources are automatically released back to the
system
线程需要调用join去释放资源,除非是detached状态,detached状态会自动释放。这个状态是create是设定的默认为joinable。

以上是关于pthread_join / __pthread_internal_find 函数发生SIGABRT的crash (Android)的主要内容,如果未能解决你的问题,请参考以下文章

POSIX线程库:pthreads

pthread_join的介绍

windows 下有没有类似linux中的pthread_detach,pthread_join和pthread_joinable函数

调用 pthread_join() 两次时 glibc pthread_join 崩溃

SylixOS pthread_join退出

pthread_join 和 pthread_mutex_lock 有啥区别?