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.
  • 11
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值