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。

猜你喜欢

转载自blog.csdn.net/ZhaoBuDaoFangXia/article/details/102827086