pthread detach和joinable的资源释放

记录一下最近看android bionic pthread相关源码的理解

这里以android kikat bionic为例

先看一下pthread_create函数源码

int pthread_create(pthread_t* thread_out, pthread_attr_t const* attr,
                   void* (*start_routine)(void*), void* arg) {
  ErrnoRestorer errno_restorer;

  // Inform the rest of the C library that at least one thread
  // was created. This will enforce certain functions to acquire/release
  // locks (e.g. atexit()) to protect shared global structures.
  // This works because pthread_create() is not called by the C library
  // initialization routine that sets up the main thread's data structures.
  __isthreaded = 1;

  pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(calloc(sizeof(*thread), 1));          使用calloc创建thread结构体,初始化为0
  if (thread == NULL) {
    __libc_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: couldn't allocate thread");
    return EAGAIN;
  }
  thread->allocated_on_heap = true;    标记thread结构体是在heap上创建的,也提示了之后不使用的时候要用free去销毁

  if (attr == NULL) {
    pthread_attr_init(&thread->attr);  如果创建线程没有指定attr则使用default的attr
  } else {
    thread->attr = *attr;              如果有指定attr,则使用指定的attr
    attr = NULL; // Prevent misuse below.
  }

  // Make sure the stack size and guard size are multiples of PAGE_SIZE.
  thread->attr.stack_size = (thread->attr.stack_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
  thread->attr.guard_size = (thread->attr.guard_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);

  if (thread->attr.stack_base == NULL) {
    // The caller didn't provide a stack, so allocate one.
    thread->attr.stack_base = __create_thread_stack(thread);  如果没有指定attr的stack_base,那么自己创建一个
    if (thread->attr.stack_base == NULL) {
      free(thread);
      return EAGAIN;
    }
  } else {
    // The caller did provide a stack, so remember we're not supposed to free it.
    thread->attr.flags |= PTHREAD_ATTR_FLAG_USER_STACK;  指定一个flag,之后由user自己释放这块地址
  }

  // Make room for the TLS
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pthread_detach()和pthread_join()都是用于管理线程的函数,但它们有一些区别。 pthread_detach()函数用于将一个已创建的线程标记为可分离的,这意味着一旦线程结束,它的资源将会自动被回收。被标记为可分离的线程不需要被其他线程等待或回收资源,因此在主线程无需调用pthread_join()来等待分离的线程结束。 示例代码如下: ```c #include <pthread.h> void *thread_func(void *arg) { // 线程执行的代码 return NULL; } int main() { pthread_t thread; pthread_create(&thread, NULL, thread_func, NULL); pthread_detach(thread); // 标记线程为可分离的,无需等待回收 // 主线程继续执行其他任务 return 0; } ``` pthread_join()函数用于等待一个指定的线程结束,并回收其资源。当一个线程通过pthread_join()被其他线程等待时,它会进入阻塞状态,直到被等待的线程结束。一旦目标线程结束,pthread_join()函数会返回,并将目标线程的返回值传递给调用者。 示例代码如下: ```c #include <stdio.h> #include <pthread.h> void *thread_func(void *arg) { // 线程执行的代码 return (void *)42; } int main() { pthread_t thread; pthread_create(&thread, NULL, thread_func, NULL); void *thread_return; pthread_join(thread, &thread_return); // 等待线程结束并回收资源 printf("Thread returned: %d\n", (int)thread_return); return 0; } ``` 总结来说,pthread_detach()适用于不需要等待线程结束的情况,而pthread_join()适用于需要等待线程结束并获取返回值的情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值