Android(bionic) pthread_id返回是一个负数

pthread_create 在Android和Linux下返回的thread_id一个为正数一个为负数

Q

因需要移植linux下模块到android平台上去,由于判断了返回值和thread_id(是否大于0),导致模块线程无法正常启动(pthread_create返回成功)。

Android

通过分析Android源码(bionic),pthread_id是由指针强转为长整型数,如下:

 // Publish the pthread_t and unlock the mutex to let the new thread start running.
  *thread_out = __pthread_internal_add(thread);
  
  
  pthread_t __pthread_internal_add(pthread_internal_t* thread) 
  {
      ScopedWriteLock locker(&g_thread_list_lock);
      // We insert at the head.
      thread->next = g_thread_list;
      thread->prev = nullptr;
      if (thread->next != nullptr) {
        thread->next->prev = thread;
      }
      g_thread_list = thread;
      return reinterpret_cast<pthread_t>(thread); //指针强转, typedef long pthread_t;
}

Linux

分析Linux源码(glibc),pthread_id是强转为无符号长整型的数,如下:

  /* Pass the descriptor to the caller.  */
  *newthread = (pthread_t) pd; // 指针强转,typedef unsigned long int pthread_t;

分析完源码发现果然问题都是自找的,官方手册也并没有说pthread_id返回一定是大于正整数,pthread_create返回0即成功:

在这里插入图片描述

so

判断返回值即可,无需判断thread_id!!!!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值