开发者社区> 问答> 正文

linux C 线程终止问题,着急,求各位大神帮忙

include
#include
#include
static int run = 1;
static int retvalue;
void *start_routine(void *arg){
int *runing = arg;
printf("The child thread has been inited,the arg is: %d\n",*runing);
while(*runing){
printf("The child thread is runing !\n");
usleep(1);
}
printf("Exit child thread\n");
retvalue = 8;
pthread_exit((void *)&retvalue);
}
int main(void){
pthread_t pt;
int ret = -1;
int times = 3;
int i = 0;
int ret_join = NULL;
ret = pthread_create(&pt,NULL,(void)start_routine,&run);
if(ret !=0){
printf("Create thread fail!\n");
return 1;
}
usleep(1);
for(;i < times;i++){
printf("Print the main thread!\n");
usleep(1);
}
run = 0;
pthread_join(pt,(void*)&ret_join);
printf("The return of thread is %d\n",*ret_join);
return 0;
}

在子线程中,while循环是一个死循环,那个子线程如何退出终止的?各位大神,求解答!谢了各位!总是在线程这里玩儿不明白...

展开
收起
杨冬芳 2016-07-13 16:41:48 1871 0
1 条回答
写回答
取消 提交回答
  • IT从业

    哥们,子线程哪是一个死循环呢。。。 子线程循环条件就是run>0, 程序开始运行后,因为usleep()作用在主线程和线程之间切换执行,主线程执行到run=0后,子线程的循环条件也不存在就结束了呗

    2019-07-17 19:55:47
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载
ECS运维指南 之 Linux系统诊断 立即下载