_张鹏鹏_ 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条回答

    报告相同问题?

    悬赏问题

    • ¥50 树莓派安卓APK系统签名
    • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题
    • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
    • ¥20 求一个html代码,有偿
    • ¥100 关于使用MATLAB中copularnd函数的问题
    • ¥20 在虚拟机的pycharm上
    • ¥15 jupyterthemes 设置完毕后没有效果
    • ¥15 matlab图像高斯低通滤波