关于使用pthread_detach后出现Segmentation fault的问题

俞二侠 俞二侠 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我在函数中加入了线程分离,之后主线程进入while(1)循环,可是运行程序后报出的错误是Segmentation fault,请各位大佬帮我看看问题出现在哪里?谢谢了!!

void *mythread1(void)
{
int i;
pthread_detach(pthread_self());
for(i=0;i<10;i++)
{
printf("this is the 1 pthread,created by zhouzheng!\n");
sleep(1);
}
pthread_exit(NULL);
}

void *mythread2(void)
{
int i;
pthread_detach(pthread_self());
for(i=0;i<10;i++)
{
printf("this is the 2 pthread,created by zhouzheng!\n");
sleep(1);
}
pthread_exit(NULL);
}

int main()
{
int i=0;
int ret =0;
pthread_t id1,id2;
ret = pthread_create(&id1,NULL,(void*)mythread1,NULL);
if(ret) {
printf("Create pthread error!\n");
return 1;
}
ret = pthread_create(&id2, NULL, (void*)mythread2,NULL);
if(ret) {
printf("Create pthread error!\n");
return 1;
}
// pthread_join(id1,NULL);
// pthread_join(id2,NULL);
for(;;)
{
printf("this is main pthread\n");
sleep(1);
}
printf("Programme is end!\n");
return 0;
}

...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
俞二侠
C语言 70,039 成员 243,237 内容