_张鹏鹏_ 2017-09-01 02:18
浏览 405
已结题

Linux线程分离后主线程连接怎么会成功?

源代码如下:
#include
#include
#include
void *thread1(void *arg);
void *thread2(void *arg);
void main(){
int err1,err2;
pthread_t tid1,tid2;
void *retval1,*retval2;
//pthread_create()成功返回0
err1 = pthread_create(&tid1,NULL,thread1,NULL);
err2 = pthread_create(&tid2,NULL,thread2,NULL);
printf("main thread begin\n");
if(err1 || err2){
printf("create thread err\n");
return 0;
}
//pthread_join()成功返回0,否则返回错误码
printf("th1 join state:%d\n",pthread_join(tid1,&retval1));
printf("th2 join state:%d\n",pthread_join(tid2,&retval2));
printf("th1 return val:%d\n",(void *)retval1);
printf("th2 return val:%d\n",(void *)retval2);
printf("main thread end\n");
}

void *thread1(void *arg){
printf("hello I am thread1\n");
return (void *)1;
}
void *thread2(void *arg){
printf("hello I am thread2\n");
pthread_detach(pthread_self());
return (void *)2;
}

运行结果:
第二幅图为什么会运行正常?线程分离后连接不是会失败的吗?
图片说明

图片说明

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 jupyterthemes 设置完毕后没有效果
    • ¥15 matlab图像高斯低通滤波
    • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
    • ¥15 钢筋实图交点识别,机器视觉代码
    • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
    • ¥50 400g qsfp 光模块iphy方案
    • ¥15 两块ADC0804用proteus仿真时,出现异常
    • ¥15 关于风控系统,如何去选择
    • ¥15 这款软件是什么?需要能满足我的需求
    • ¥15 SpringSecurityOauth2登陆前后request不一致