pthread_jion() , pthread_create() 导致的segmentation fault

本文档分析了一个因使用pthread_create和pthread_join不当导致的segmentation fault问题。在main函数中,pthread_create的返回值被错误地赋给了sock_listen_tid,导致在调用pthread_join时出现问题。当移除pthread_join后,程序运行正常。解决办法是正确处理pthread_create的返回值,并在pthread_join中传入正确的线程ID。
部署运行你感兴趣的模型镜像

下午调试程序的“segmentation fault”,先上代码:

 

 

左看右看找不到错在那,尝试去掉pthread_join, 竟然正常了。

 

注意这句:

sock_listen_tid = pthread_create(&sock_listen_tid,NULL,listen_thread,NULL);

 

pthread_create的返回值赋给了sock_listen_tid,这行代码看起来的确的确的确有些怪~

 

看函数说明 man 3 pthread_create

 

函数返回值描述如下:

 

RETURN VALUE

       If successful, the pthread_create() function shall return  zero;  otherwise,

       an error number shall be returned to indicate the error.


ERRORS

       The pthread_create() function shall fail if:


       EAGAIN The  system  lacked the necessary resources to create another thread,

              or the system-imposed limit on the  total  number  of  threads  in  a

              process {PTHREAD_THREADS_MAX} would be exceeded.


       EINVAL The value specified by attr is invalid.


       EPERM  The  caller  does not have appropriate permission to set the required

              scheduling parameters or scheduling policy.


       The pthread_create() function shall not return an error code of [EINTR].


       The following sections are informative.

 

 

如果创建成功, pthread_create将返回0. 赋值给sock_listen_tid等于0。然后执行pthread_join(0, NULL),相当于等待线程号为0的线程,当然segmentation fault

 

 

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值