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

5 篇文章 0 订阅
1 篇文章 0 订阅

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。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值