pthread在自己创建的线程中执行pthread_jion并不会死锁

#include <stdio.h>
#include <pthread.h>
pthread_t tid;

void *func(void *args) {
  printf("Thread running...");
  pthread_join(tid, NULL);
  printf("Thread waiting...");
  return NULL;
}

int main(void) {
  printf("Hello World\n");
  pthread_create(&tid, NULL, func, NULL);
  pthread_join(tid, NULL);
  return 0;
}

pthread_join:

函数pthread_join()函数挂起调用线程的执行,直到目标线程之三结束为止,除非目标线程已经终止。

但是上述代码实际并不会死锁。

原因:

pthread_join(3) - Linux manual page

实际上这种情况pthread_join会直接返回EDEADLK,并不会一直阻塞!!!

RETURN VALUE         top

       On success, pthread_join() returns 0; on error, it returns an
       error number.

ERRORS         top

       EDEADLK
              A deadlock was detected (e.g., two threads tried to join
              with each other); or thread specifies the calling thread.

       EINVAL thread is not a joinable thread.

       EINVAL Another thread is already waiting to join with this
              thread.

       ESRCH  No thread with the ID thread could be found.

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 20:40:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 20:40:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 20:40:05       82 阅读
  4. Python语言-面向对象

    2024-06-16 20:40:05       91 阅读

热门阅读

  1. Linux C语言:指针函数、递归函数及用法

    2024-06-16 20:40:05       30 阅读
  2. 离线数仓VS实时数仓

    2024-06-16 20:40:05       27 阅读
  3. 对冲基金为什么叫做Hedge Fund?

    2024-06-16 20:40:05       36 阅读
  4. GStreamer安装——Linux

    2024-06-16 20:40:05       33 阅读
  5. 工业化正在创造千篇一律的味道

    2024-06-16 20:40:05       29 阅读